Skip to content

Instantly share code, notes, and snippets.

@AndersDJohnson
Last active June 5, 2019 13:33
Show Gist options
  • Select an option

  • Save AndersDJohnson/4249568 to your computer and use it in GitHub Desktop.

Select an option

Save AndersDJohnson/4249568 to your computer and use it in GitHub Desktop.
Object functional js
// Like underscore.js "filter", but returns an object with the keys and values
// instead of just an array of values.
Object.prototype.filter = function(obj, predicate) {
var result = {}, key;
for (key in obj) {
if (obj.hasOwnProperty(key) && predicate(obj[key], key, obj)) {
result[key] = obj[key];
}
}
return result;
};
// Like underscore.js "map", but returns an object with the keys and mapped values
// instead of just an array of mapped values.
Object.prototype.map = function(obj, fn) {
var result = {}, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
result[key] = fn(obj[key], key, obj);
}
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment