Explanation : Child component method has been called from parent component with an array of objects
as a parameter. In child component, assigning this array
into a variable.
Parent component :
let obj = [{
'prop1': 'value1',
'prop2': 'value2'
},{
'prop1': 'value1',
'prop2': 'value2'
}];
this.child.sendObject(obj);
Child component :
public sendObject(obj) : void {
this.updatedObj = obj;
}
Problem statement :
Properties of the objects passed from parent component can be updated and send to child component.
Parent component :
let obj = [{ 'prop1': 'value4', 'prop2': 'value2' },{ 'prop1': 'value1', 'prop2': 'value7' }]; this.child.sendObject(obj);
New objects can be added in the child component itself in the
obj
array.Child component :
public sendObject(obj) : void { this.updatedObj = obj; } public addNewObj() { this.updatedObj.push({ 'prop3': 'value3', 'prop4': 'value4 }) }
I want to validate (check equality) the previously send object with this updated object.
What I tried so far ?
Child component :
public sendObject(obj) : void {
const copy = JSON.parse(JSON.stringify(obj));
this.updatedObj = obj;
if (JSON.stringify(this.updatedObj) === JSON.stringify(copy)) {
console.log("Not changed");
} else {
console.log("Changed");
}
}
Above logic is working fine if the objects properties modified from the parent but if i added any new object into the existing array (this.updatedObj
) in the child component and switches back from parent to child. It breaks and shows only objects passed from the parent.