is there a faster way to transform this:
[
{ name: 'name1', value: 'value1' },
{ name: 'name2', value: 'value2' },
{ name: 'name3', value: 'value3' },
...
]
into this:
{
name1: value1,
name2: value2,
name3: value3,
...
}
without manually looping and filling an object?
could the underscore library have a way to do this? it is already in our application, so
it can be used without an extra cost.
currently i have a manual action,
though i’m not sure it is the best performance i can have.
var data = []; // see the array above
var result = {};
for(var i = 0; i < data.length; i++)
{
var o = data[i];
result[o.name] = o.value;
}
I don’t think there is a faster way. With underscore there is a shorter way, but it’s almost certainly slower than your loop: