If you want to merge 2 arrays of objects in JavaScript. You can use this one line trick
Array.prototype.push.apply(arr1,arr2);
For Example
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); // final merged result will be in arr1
Output:
[{"name":"lang","value":"English"},
{"name":"age","value":"18"},
{"name":"childs","value":"5"},
{"name":"lang","value":"German"}]
Answer from Jahanzaib Aslam on Stack OverflowIf you want to merge 2 arrays of objects in JavaScript. You can use this one line trick
Array.prototype.push.apply(arr1,arr2);
For Example
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); // final merged result will be in arr1
Output:
[{"name":"lang","value":"English"},
{"name":"age","value":"18"},
{"name":"childs","value":"5"},
{"name":"lang","value":"German"}]
With ES6 you can do it very easy as below:
var arr1 = new Array({name: "lang", value: "German"}, {name: "age", value: "18"});
var arr2 = new Array({name : "childs", value: '5'}, {name: "lang", value: "German"});
var arr3 = [...arr1, ...arr2];
Output:
arr3 = [
{"name":"lang","value":"German"},
{"name":"age","value":"18"},
{"name":"childs","value":"5"},
{"name":"lang","value":"German"}
]
Hello, I'm currently facing this problem where I have to sorta merge 2 array of objects, in one of my arrays I have a count of messages sent while in the other I have visits to a URL, I have to use this 2 properties to perfom some calculations, they both share a common a property which I can use to "link" together.
I need to combine both into one array where each "day" property conincide. This is the structure.
{
"MessageCount": [
{
"day": "07-23-2018",
"count": 0
},
{
"day": "07-24-2018",
"count": 12
}
],
"LinkCount": [
{
"day": "07-23-2018",
"visits": 12
},
{
"day": "07-24-2018",
"visits": 10
}
}I could also work with advice with the query I'm using, maybe I could build the array instead of doing a merge. I'm doing pretty much 2 identical queries to different MongoDB collections to build those arrays.
statistics.smsSentCount = await Promise
.all(collections.map((collection) =>
collection.smsSent.collection.count(filters).exec()
.then((count) => {
return {
day: collection.smsSent.day,
count,
};
})));Here's what I mean. I want the merged array to be created such that contents of arrayTwo[0] are inserted into arrayOne[0], contents of arrayTwo[1] are inserted into arrayOne[1], and so on. How can I accomplish this? I hope it's possible.
Pretty simple if you use ES2018
const resultArray = arr1.map(t1 => ({...t1, ...arr2.find(t2 => t2.id === t1.id)}))
You can convert one array into an object and then iterate over the other to combine both. This way you can have O(n) time complexity.
const arr1 = [
{
"country_id": null,
"created_at": "2020-11-13T04:30:16.000000Z",
"id": 1,
"updated_at": "2020-11-13T04:30:16.000000Z"
}
];
const arr2 = [
{
"country_id": null,
"created_at": "2020-11-13T04:30:16.000000Z",
"id": 1,
"name": "1111",
"photo": null,
"surname": "111",
"updated_at": "2020-11-13T04:30:16.000000Z"
}
];
const dictionary = arr2.reduce((acc, curr) => ({
...acc,
...(curr.id && { [curr.id]: curr })
}), {});
const mappedArray = arr1
.map(itemFromFirst => ({
...itemFromFirst, ...dictionary[itemFromFirst.id]
})
);
/* Result:
[{
"country_id": null,
"created_at": "2020-11-13T04:30:16.000000Z",
"id": 1,
"updated_at": "2020-11-13T04:30:16.000000Z",
"name": "1111",
"photo": null,
"surname": "111"
}]
*/
Here is one option:-
var array = [{
name: "foo1",
value: "val1"
}, {
name: "foo1",
value: ["val2", "val3"]
}, {
name: "foo2",
value: "val4"
}];
var output = [];
array.forEach(function(item) {
var existing = output.filter(function(v, i) {
return v.name == item.name;
});
if (existing.length) {
var existingIndex = output.indexOf(existing[0]);
output[existingIndex].value = output[existingIndex].value.concat(item.value);
} else {
if (typeof item.value == 'string')
item.value = [item.value];
output.push(item);
}
});
console.dir(output);
Here is another way of achieving that goal:
var array = [{
name: "foo1",
value: "val1"
}, {
name: "foo1",
value: [
"val2",
"val3"
]
}, {
name: "foo2",
value: "val4"
}];
var output = array.reduce(function(o, cur) {
// Get the index of the key-value pair.
var occurs = o.reduce(function(n, item, i) {
return (item.name === cur.name) ? i : n;
}, -1);
// If the name is found,
if (occurs >= 0) {
// append the current value to its list of values.
o[occurs].value = o[occurs].value.concat(cur.value);
// Otherwise,
} else {
// add the current item to o (but make sure the value is an array).
var obj = {
name: cur.name,
value: [cur.value]
};
o = o.concat([obj]);
}
return o;
}, []);
console.log(output);