How to convert an array of objects to an object in Lodash? How to convert an array of objects to an object in Lodash? arrays arrays

How to convert an array of objects to an object in Lodash?


I think an even shorter solution would be:

Object.assign({}, ...array)

I know you asked for lodash, but it doesn't seem like you even need this way. Unless you want to use _.extend.


_.reduce(array, function(memo, current) { return _.assign(memo, current) },  {})


Here's a shorter version:

_.transform(array, _.ary(_.extend, 2),  {});

The transform() function is like reduce(), except it's not expecting you to return anything. Since extend() is altering it's first argument, we can just pass it straight to transform(). It's wrapped in ary() to make sure it only gets 2 arguments passed to it.