Skip to content

Instantly share code, notes, and snippets.

@jonkemp
Created March 4, 2014 21:36
Show Gist options
  • Save jonkemp/9356302 to your computer and use it in GitHub Desktop.
Save jonkemp/9356302 to your computer and use it in GitHub Desktop.
Compare 2 objects based on their keys: WIP
var _ = _ || {};
_.contains = function (obj, target) {
if (obj == null) return false;
if (Array.prototype.indexOf && obj.indexOf === Array.prototype.indexOf) return obj.indexOf(target) != -1;
return obj.some(function(value) {
return value === target;
});
};
_.difference = function (array) {
var rest = array.concat(Array.prototype.slice.call(arguments, 1));
return array.filter(function(value){ return !_.contains(rest, value); });
};
_.diffObjKeys = function (obj1, obj2) {
var keys1 = Object.keys(obj1);
var keys2 = Object.keys(obj2);
return difference(keys1, keys2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment