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

Need Your Help

how to start a program using a windows service?

c++ windows visual-studio service exe

I created a windows service in c++ using visual studios and now I want the service to run an exe file. The service is set to start every time the computer starts

double boost::bind cause compile-time error

c++ boost bind functor boost-bind

The following code give compile error caused by line 17: