Compare two objects if any one of the object not having key pair.
var itemA= [{"name": "saran", "address":"chennai"},
{"name": "elango", "address":"chennai"},
{"name": "kala", "address": "chennai"}];
var itemB= [{"name": "saran", "address":"chennai"},
{"name": "elango", "address":"chennai"}];
I wrote following code to compare two objects,
function compareJSON(itemA, itemB) {
for(var prop in itemA) {
if(itemB.hasOwnProperty(prop)) {
switch(typeof(itemA[prop])) {
case "object":
compareJSON(itemA[prop], itemB[prop]);
break;
default:
if(itemA[prop] !== itemB[prop]) {
}
break;
}
}
}
}
Here i have two object i need to compare above two object. I used to for loop for compare two object with hasOwnProperty() method. the prop is having itemA of object it's checked with itemB object if it's different i took itemA object.
Here problem is not able compare itemA 3rd value not able to compare because in itemB does not have a 3rd element.