How to efficiently concatenate two arrays in Javascript?
Let's say I have two arrays of objects in Javascript:
var myList = [{id: 3, info: 'bla'}, {id: 97, info: 'ble'}, {id: 25, info: 'blu'}];
var newList = [{id: 5, info: 'blo'}, {id: 3, info: 'different Info!!'}];
Now I want to "unite" newList
in the myList
in the sense that:
- all objects with an id already present in
myList
must be replaced with an object innewList
- all objects with an id not present in
myList
must be appended fromnewList
tomyList
However, I don't understand how I can check if an object with a specific ID is already in the array. I think you could loop over myList for each item in the newList, but that is not very scalable.
Does anyone know how I can do this efficiently? All advice is appreciated!
source to share
Try:
function combineArray(array1, array2) {
var arr = array1.concat(array2);
for(var i = 0; i < arr.length; ++i) {
for(var j = i + 1; j < arr.length; ++j) {
if(arr[i].id === arr[j].id)
arr.splice(i, 1);
}
}
return arr;
};
Then
combineArray([{id: 3, info: 'bla'}, {id: 97, info: 'ble'}, {id: 25, info: 'blu'}], [{id: 5, info: 'blo'}, {id: 3, info: 'different Info!!'}] );
returns
[Object { id=97, info="ble"}, Object { id=25, info="blu"}, Object { id=5, info="blo"}, Object { id=3, info="different Info!!"}]
source to share
You can use an object to make sure the ID is unique and that will automatically overwrite the existing IDs and then convert them back to an array
var myList = [{id: 5, info: 'bla'}, {id: 97, info: 'ble'}, {id: 25, info: 'blu'}];
var newList = [{id: 5, info: 'blo'}, {id: 3, info: 'different Info!!'}];
var o = {};
var newArr = [];
myList.forEach(function(x) {
o[x.id] = x;
});
newList.forEach(function(x) {
o[x.id] = x;
});
for (var key in o) {
newArr.push(o[key])
}
document.body.innerHTML = '<pre>' + JSON.stringify(newArr, null, 4) + '</pre>';
source to share
I'm sorry that you would need to do some kind of iteration to compare your ids. You can iterate over the first array of objects to get the ID attribute of each element. Within that iteration, you could do another iteration over the concatenation of the array, comparing the values ββand pushing the desired values ββto the new "result" array.
Maybe this will help you keep your code in order with nested iterations:
Find value in array of objects in Javascript
Hope it helped.
source to share
You can go through the check newList
if it myList
has an object with this identifier. If so, replace the old one with the new one with splice
, otherwise insert the new object into myList
.
var myList = [{id: 3, info: 'bla'}, {id: 97, info: 'ble'}, {id: 25, info: 'blu'}];
var newList = [{id: 5, info: 'blo'}, {id: 3, info: 'different Info!!'}];
newList.forEach(function(el) {
var found = myList.filter(function(obj) {
return obj.id == el.id;
});
if (found.length) {
myList.splice(myList.indexOf(found[0]), 1, el);
}
else {
myList.push(el);
}
});
alert( JSON.stringify(myList, null, '\t') );
source to share
You can use the array.prototype.concat()
and methods for this array.prototype.filter()
.
var myList = [{id: 3, info: 'bla'}, {id: 97, info: 'ble'}, {id: 25, info: 'blu'}];
var newList = [{id: 5, info: 'blo'}, {id: 3, info: 'different Info!!'}];
var temp = {};
var output = newList.concat(myList).filter(function(v){
if (temp[v.id]){
return false;
}
temp[v.id] = true;
return true;
});
console.log(output);
source to share