Last active
August 29, 2015 14:01
-
-
Save danieldunderfelt/4525d19f2fa761d9ea77 to your computer and use it in GitHub Desktop.
Compare keys and values of two objects
This file contains hidden or 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
/** | |
* Use this to compare one object's properties to corresponding properties on another object. | |
* Returns true if b contains all of the properties of a with the same values. Object b | |
* can contain more properties than object a, the important thing is that the values | |
* and property names of a are found on b. Enumerable properties only! | |
*/ | |
function compare(a, b) { | |
var isEqual = []; | |
var keys = Object.keys(a); | |
keys.forEach(function(prop, id, obj) { | |
if( a[prop] === b[prop] ) isEqual.push(true); | |
}); | |
return isEqual.length === keys.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment