Merge multiple objects inside the same array into one object

0
304
var arrObj = [{a:1, b:2},{c:3, d:4},{e:5, f:6}];

how can i merge this into one obj?

//mergedObj = {a:1, b:2, c:3, d:4, e:5, f:6}

If your environment supports Object.assign, then you can do the same in a succinct way like this

const arrObj = [{a: 1, b: 2}, {c: 3, d: 4}, {e: 5, f: 6}];

console.log(arrObj.reduce(function(result, current) {
  return Object.assign(result, current);
}, {}));

// If you prefer arrow functions, you can make it a one-liner ;-)
console.log(arrObj.reduce(((r, c) => Object.assign(r, c)), {}));

// Thanks Spen from the comments. You can use the spread operator with assign
console.log(Object.assign({}, ...arrObj));

You could use reduce for an elegant solution:

arrObj.reduce(function(acc, x) {
    for (var key in x) acc[key] = x[key];
    return acc;
}, {});

LEAVE A REPLY

Please enter your comment!
Please enter your name here