Created
April 16, 2014 10:43
-
-
Save willbroderick/10850927 to your computer and use it in GitHub Desktop.
Check if two 1-level-deep json arrays contain the same values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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