I'm trying to turn this:
[ { '231634908': 137875 },
{ '388252786': 150004 },
{ '333624027': 144107 },
{ '382758108': 149729 },
{ '384113458': 149803 },
{ '384844004': 149848 },
{ '405877005': 150481 },
{ '405877005': 150481 } ]
Into this:
{
'231634908': 137875,
'388252786': 150004,
'333624027': 144107,
'382758108': 149729,
'384113458': 149803,
'384844004': 149848,
'405877005': 150481,
'405877005': 150481
}
Using underscore.
I tried
_.object(list);
_.object(_.keys(list), _.values(list));
_.object(_.keys(list[0]), _.values(list[0]));
I'm trying to turn this:
[ { '231634908': 137875 },
{ '388252786': 150004 },
{ '333624027': 144107 },
{ '382758108': 149729 },
{ '384113458': 149803 },
{ '384844004': 149848 },
{ '405877005': 150481 },
{ '405877005': 150481 } ]
Into this:
{
'231634908': 137875,
'388252786': 150004,
'333624027': 144107,
'382758108': 149729,
'384113458': 149803,
'384844004': 149848,
'405877005': 150481,
'405877005': 150481
}
Using underscore.
I tried
_.object(list);
_.object(_.keys(list), _.values(list));
_.object(_.keys(list[0]), _.values(list[0]));
Share
Improve this question
asked Nov 20, 2013 at 18:07
ThomasReggiThomasReggi
59.4k97 gold badges257 silver badges459 bronze badges
1
- Possible duplicate of How do I convert array of Objects into one Object in JavaScript? – Herohtar Commented May 9, 2019 at 3:20
6 Answers
Reset to default 17I'm no expert on underscore.js, but try this:
_.extend.apply(null, list);
One caveat: this will actually modify the first element of the list. If this is a concern you might want to use something like this instead:
_.extend.apply(null, [{}].concat(list));
You want _.reduce()
:
_.reduce(list, function(memo, o) {
var k = Object.keys(o)[0];
memo[k] = o[k];
return memo;
}, {});
A more elegant and native way to do it.
var a = [ { '231634908': 137875 },
{ '388252786': 150004 },
{ '333624027': 144107 },
{ '382758108': 149729 },
{ '384113458': 149803 },
{ '384844004': 149848 },
{ '405877005': 150481 },
{ '405877005': 150481 } ];
var b = {};
Array.prototype.forEach.call(a,function(elem) {
var keys = Object.keys(elem);
b[keys[0]] = elem[keys[0]];
});
One more native solution for case with multiple fields per object
var objects = [ { '231634908': 137875 },
{ '388252786': 150004 },
{ '333624027': 144107 },
{ '382758108': 149729 },
{ '384113458': 149803 },
{ '384844004': 149848 },
{ '405877005': 150481 },
{ '405877005': 150481 } ]
var singleObject = {};
for(var i in objects){
var oKeys = Object.keys(objects[i]);
for(var j in oKeys){
singleObject[oKeys[j]] = objects[i][oKeys[j]];
}
}
console.log(singleObject);
I know this is really old, but it inspired me to do this natively one-line for multi key objects:
var arrayOfObjectsToCombine = [ { foo: 'bar', dog: 'cat' }, { baz: 'boom' } ];
arrayOfObjectsToCombine.reduce(function(done, obj) { Object.keys(obj).map( function(K) { done[K] = obj[K] } ); return done; }, {})
result: { foo: 'bar', dog: 'cat', baz: 'boom' }
You could use Object.assign()
and spread syntax like this:
let array = [ { '231634908': 137875 },
{ '388252786': 150004 },
{ '333624027': 144107 },
{ '382758108': 149729 },
{ '384113458': 149803 },
{ '384844004': 149848 },
{ '405877005': 150481 },
{ '405877005': 150481 } ]
const output = Object.assign({}, ...array)
console.log(output)