I'm trying to compare two objects with the purpose of logging out the ones which do not exist in the first object.
First Object
this.Object1 =[{id:1, name,'Coke'}, {id:2, name,Fanta},{id:3, name,'Sprite'}, {id:4, name,'Pepsi'}]
Second Object
this.Object 2 = [{id:1, name,'Coke'}, {id:2, name,'Fanta'},{id:5, name,'Miranda'}, {id:6, name,'Alvaro', id:7, 'Orange Juice'}]
What I want to achieve is to loop through object1
and find their ids
that don't exist in object2
and push those in there
so the final result of object1
will be like this
[{id:1, name,'Coke'}, {id:2, name,Fanta},{id:3, name,'Sprite'}, {id:4, name,'Pepsi',id:5, name,'Miranda'}, {id:6, name,'Alvaro', id:7, 'Orange Juice'}]
Script
for (let r = 0; r < this.object1.length; r++) {
for (let i = 0; i < this.object2.length; i++) {
if (this.object1.id != this.object2[i].id) {
this.object2.push(this.object1[r]);
console.log(this.object1[i].name + ' does not exist');
}
}
}