DEV Community

Discussion on: How to convert an array into an object in javascript

Collapse
 
__abshir profile image
Abshir

Thank you, this was very handy.
I remixed it a little:

const convertArrayToObject = (array) => {
    return _.reduce(array, (accumulator, currentValue) => {
        accumulator = Object.assign(accumulator, currentValue);
        return accumulator;
    }, {});
};
Enter fullscreen mode Exit fullscreen mode
Collapse
 
__abshir profile image
Abshir

I should have mentioned that I've used the lodash reduce method here instead of the javascript reduce