Monday, May 20, 2024
180
rated 0 times [  187] [ 7]  / answers: 1 / hits: 63375  / 11 Years ago, tue, july 23, 2013, 12:00:00

I've been going through the underscore docs but I can't seem to find a method (or nested method call) to do the following transformation:



Let's say I have the following Javascript array:



 [{ name : sEcho, value : 1},{ name : iColumns, value : 12}, ... ]


And I need to transform it into the following object:



 {
sEcho: 1,
iColumns: 12,
...
}


I'm using underscore.js for a reason so it must be a one liner.


More From » underscore.js

 Answers
45

Since nobody has posted this as an answer, I'm posting it because I think it is better than Jan's answer. It's shorter, and cleaner without the inline function.



_.object(_.pluck(data, 'name'), _.pluck(data, 'value'));

[#76812] Sunday, July 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kyla

Total Points: 77
Total Questions: 108
Total Answers: 111

Location: Grenada
Member since Mon, May 8, 2023
1 Year ago
;