Skip to content

Instantly share code, notes, and snippets.

@VictorQueiroz
Last active August 29, 2015 14:04
Show Gist options
  • Save VictorQueiroz/a8f49365943d807012f8 to your computer and use it in GitHub Desktop.
Save VictorQueiroz/a8f49365943d807012f8 to your computer and use it in GitHub Desktop.
Remove from object all keys who aren't on the array.
(function(window) {
var only = function (keep) {
var obj = this;
Object.keys(obj).forEach(function(key) {
if(keep.indexOf(key) === -1)
obj.delete(key);
});
return obj;
};
Object.prototype.delete = function (name) {
delete this[name];
};
if ('defineProperty' in Object) {
Object.defineProperty(Object.prototype, 'only', {
value: only,
enumerable: false
});
} else {
Object.prototype.only = only;
}
})(window);
@darlanalves
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment