Convert array of objects to just one object

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]));

Answers


I'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)

Need Your Help

Azure ClearDB: Access denied for user 'xxxx' to database 'xxxx'

java mysql database azure cleardb

So I recently set up a database through Microsoft Azure using clearDB's MySQL db.

How can i get the image source from the other site?

php arrays web-scraping output preg-match-all

I had so many web site addresses which have images I want to have.