I have two array of objects
this.originalData = [
{age: 27, name: "sachin", id: 1, sex: "male", dept: "angular"},
{age: 22, name: "pooja", id: 2, sex: "female", dept: "java"},
{age: 50, name: "john", id: 3, sex: "male", dept: "sales"}
]
this.updatedData = [
{id: 1, name: "sachin", age: 25, sex: "male"},
{id: 2, name: "pooja", age: 22, sex: "female"},
{id: 3, name: "john", age: 50, sex: "male"}
]
As we can see the order and number of properties is different in both the arrays. Here, how can I do the comparison for only matching properties whether any of it is changed. In the above example, I need to get the object with id 1
from updatedData
as the age
property is changed from 27
to 25
when compared with originalData
. The properties which are not matching can be ignored.
I tried like below but it is not working due to the differences
if(JSON.stringify(this.updatedData) !== JSON.stringify(this.originalData)) {
console.log('changed!');
}
Please suggest. Thanks.