David David - 4 years ago 114
Javascript Question

How to merge an array of object

I have one array of object.

Objs[0] = {Name : "ABC"};
Objs[1] = {Roll : 123}


I want to merge both, It will be like

Objs {
Name : "ABC",
Roll : 123
}


Any way to I achieve this?

Answer Source

You can use Object.assign method.

var Objs = [{
  Name: "ABC"
}, {
  Roll: 123
}];

console.log(
  Object.assign.apply(null, [{}].concat(Objs))
)


Or you can use spread syntax instead of Function#apply method.

var Objs = [{
  Name: "ABC"
}, {
  Roll: 123
}];

console.log(
  Object.assign({}, ...Objs)
)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download