Lets say I've got 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!!'}];
I now want to "merge" the newList
into myList
, in the sense that:
- all objects with an id already present in
myList
, should be replaced by the object innewList
- all objects with an id not present in
myList
, should be added fromnewList
tomyList
I don't understand however, how I can check if an object with a certain id is already in the array. I guess you could do a loop over myList for every item in newList, but that is not very scalable.
Does anybody know how I can efficiently do this? All tips are welcome!