Last active
August 29, 2015 14:04
-
-
Save VictorQueiroz/a8f49365943d807012f8 to your computer and use it in GitHub Desktop.
Remove from object all keys who aren't on the array.
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
(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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Funciona melhor:
https://gist.github.com/darlanalves/32edfa35937cde1ca4fb