Skip to content

Instantly share code, notes, and snippets.

@willbroderick
Created April 16, 2014 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willbroderick/10850927 to your computer and use it in GitHub Desktop.
Save willbroderick/10850927 to your computer and use it in GitHub Desktop.
Check if two 1-level-deep json arrays contain the same values
// A shallow scan of a JSON object to test for equality
function getNumKeysInJsonObject(obj) {
var i = 0;
for (var x in obj)
if (obj.hasOwnProperty(x)) i++;
return i;
}
function jsonIsEqual(obj1, obj2) {
var isEqual = true; //Equal until proven otherwise
if(typeof obj1 != 'undefined' && typeof obj2 != 'undefined') {
var numKeysInObj1 = 0;
for(var key in obj1){
if(obj1.hasOwnProperty) {
numKeysInObj1++;
if(typeof obj2[key] == 'undefined' || obj1[key] != obj2[key]) {
isEqual = false;
}
}
}
if(numKeysInObj1 != getNumKeysInJsonObject(obj2)) {
isEqual = false;
}
} else {
isEqual = false;
}
return isEqual;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment