Skip to content

Instantly share code, notes, and snippets.

@pjanuario
Created March 24, 2014 16:26
Show Gist options
  • Save pjanuario/9743752 to your computer and use it in GitHub Desktop.
Save pjanuario/9743752 to your computer and use it in GitHub Desktop.
Compare javascript objects and output property name and value of different ones. Usefull for unit testing, property detection.
var compareKeys = function(obj, obj2){
for(var key in obj){
var _obj = obj[key];
var _obj2 = obj2[key];
if(!_obj){
if(_obj2){
console.log("%s: %s => %s",
key, _obj, _obj2);
}
continue;
}
var isObject = _obj !== null
&& typeof _obj === 'object';
if (isObject){
compareKeys(_obj, _obj2);
} else {
var equals = _obj == _obj2;
if(!equals){
console.log("%s: %s => %s",
key, _obj, _obj2);
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment