2

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

6 Answers 6

17

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));
Sign up to request clarification or add additional context in comments.

2 Comments

If you wanted to make a function for this operation what would you call it? extendArray?
@ThomasReggi Probably merge or something similar.
3

You want _.reduce():

_.reduce(list, function(memo, o) {
  var k = Object.keys(o)[0];
  memo[k] = o[k];
  return memo;
}, {});

2 Comments

Some Underscore pro can probably point out that there are Underscore utilities to cut this in half :)
The body can be just return _.extend(memo, o);.
3

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

Comments

1

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

Comments

0

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' }

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.