Skip to content

Instantly share code, notes, and snippets.

@metaist
Created March 19, 2014 02:32
Show Gist options
  • Save metaist/9634482 to your computer and use it in GitHub Desktop.
Save metaist/9634482 to your computer and use it in GitHub Desktop.
Add .filter to JavaScript objects.
// Based on: http://stackoverflow.com/a/5072145/12815
Object.filter = function(obj, predicate) {
var result = {}, key;
for(key in obj) {
if (obj.hasOwnProperty(key) && predicate.call(obj, key, obj[key])) {
result[key] = obj[key];
}//end if: key added to result
}//end for: iterated over the object
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment