Created
March 24, 2014 16:26
-
-
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.
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
| 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