JSFiddle - React, Tailwind, and code Playground
JavaScript
// Check if two objects are the same
// TODO: Update to iterate over nested arrays, order of array elements should not matter
function checkSame(a, b) {
let isSame = true;
for (let prop in a) {
if (a[prop] !== b[prop]) {
// ISSUE: Returning not the same if value is an array
console.log(`${prop} not the same! ${JSON.stringify(prop)}`);
isSame = false;
}
}
return isSame;
}
let a = {
'Title': 'Example title',
'Description': 'Example description',
'Items': [{
'NestedObject1': true
}, {
'NestedObject2': true
}, {
'NestedObject3': true
}]
};
let b = {
'Title': 'Example title',
'Description': 'Example description',
'Items': [{
'NestedObject1': true
}, {
'NestedObject2': true
}, {
'NestedObject3': true
}]
};
let c = {
'Title': 'Example title',
'Description': 'Example description',
'Items': [{
'NestedObject1': true
}, {
'NestedObject2': false
}, {
'NestedObject3': true
}]
};
console.log(checkSame(a, b)); // returns false but should return true, 'Items' array of objects is not the same but it should be the same
console.log(checkSame(a, c)); // returns false but should return true