I work on an angular application and I have to compare 2 arrays of object :
ar1 = [{id: 2, itemId: 1},
{id: 5, itemId: 3},
{id: 18,itemId: 13},
{id: 16,itemId: 14}]
ar2 = [{id: 13, itemId: 1},
{id: 12, itemId: 14}]
I try to do something like this:
for(let i = 0 ; i < this.ar1.length ; i++){
if(this.ar2[i] != undefined){
if(this.ar1[i].itemId == this.ar2[i].itemId){
console.log("in and ==itemId",this.ar2[i])
}
else{
console.log("in and !=itemId",this.ar1[i])
}
}
else{
console.log("undefined",this.ar1[i])
}
}
It's return me that :
in and ==itemId {id: 13, itemId: 1}
in and !=itemId {id: 5, itemId: 3}
undefined {id: 18, itemId: 13}
undefined {id: 16, itemId: 14}
I want a function which can say me which object is in the 2 arrays and which object is not in the 2 arrays.
It's not a duplicate cause I don't want to see difference between 2 arrays of object but see if the itemId if the same and get the object with the same itemId.