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 followi
Let's say you have the following JavaScript array:
var list = [
{
name: "sEcho",
value: 1
},
{
name: "iColumns",
value: 12
},
...
];
You can convert it into the format you want as follows:
var table = _.reduce(list, function (table, item) {
table[item.name] = item.value;
return table;
}, {});
It's not a one liner, but I don't think you literally meant a one liner. Did you?
Here's a one liner if you really meant a one liner:
var t = _.reduce(list, function (t, i) { return t[i.name] = i.value, t; }, {});
Yes, others have provided the same answer. However that's only because the answer to your question is so simple.