how to merge multiple objects in array javascript code example
Example 1: javascript object array merge
var arr1 = [{name: "lang", value: "English"},{name: "age", value: "18"}];
var arr2 = [{name : "childs", value: '5'}, {name: "lang", value: "German"}];
Array.prototype.push.apply(arr1,arr2);
console.log(arr1);
Example 2: javascript create array of objects from multiple arrays
var ids = [1,2,3];
var names = ["john","doe","foo"];
var countries = ["AU","USA","USA"];
var items = ids.map((id, index) => {
return {
id: id,
name: names[index],
country: countries[index]
}
});
var items = [
{id: 1, name: "john", country: "AU"},
{id: 2, name: "doe", country: "USA"},
...
];