I need help with comparing two JavaScript objects to find the differences. The code I am using in the function is not giving me the desired output.
function compare() {
var Json1 = [
{ name: "ABC", value: "5" },
{ name: "DEF", value: "85712264" },
];
var Json2 = [
{ name: "DEF", value: "85712264" },
{ name: "ABC", value: "3" },
];
var obj1Keys = Object.keys(Json1);
var obj1Values = Object.values(Json1);
var obj2Keys = Object.keys(Json2);
var obj2Values = Object.values(Json2);
console.log(obj1Keys);
console.log(obj1Values);
for (let i = 0; i < obj1Keys.length; i++) {
for (let j = 0; j < obj2Keys.length; j++) {
if (obj1Keys[i] === obj2Keys[j]) {
if (obj1Values[i] !== obj2Values[j]) {
console.log(obj1Keys[i]);
console.log(obj1Values[i]);
}
}
}
}
}
I am attempting to compare the 'name' and 'value' from Json1 with Json2. If a matching 'name' is found, then I want to compare the 'value' for that 'name'. If there is a difference in the 'value', I would like to print the 'name' and 'value' pair from Json1.
Expected result: ABC, 5
Thank you for your assistance!