Skip to content

Instantly share code, notes, and snippets.

@FGRibreau
Last active December 27, 2015 13:39
Show Gist options
  • Save FGRibreau/7334567 to your computer and use it in GitHub Desktop.
Save FGRibreau/7334567 to your computer and use it in GitHub Desktop.
Object::filter /for @4ntoin3
var obj = {a:1,b:2,c:9,d:10,f:5};
Object.defineProperty(Object.prototype, "filter", {
value: function(f) {
return Object.keys(this).reduce(function(m, k) {
return f(this[k], k, this) ? (m[k] = this[k], m) : m;
}.bind(this), {});
},
enumerable: false,
writable: true
});
obj.filter(function(v, k, obj){return v >= 5;});
// Object {c: 9, d: 10, f: 5}
@FGRibreau
Copy link
Author

Note: writable:true is just here for testing

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