Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@noeticpenguin
Created January 6, 2014 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noeticpenguin/8288321 to your computer and use it in GitHub Desktop.
Save noeticpenguin/8288321 to your computer and use it in GitHub Desktop.
// Write a general-purpose reducer that excludes items based on a predicate
function excludeReducer(predicate) {
return function(newArray, item) {
return predicate(item) ? newArray : newArray.concat(item);
}
}
function excludeIdReducer(id) {
return excludeReducer(function(item) {
return item.id === id;
});
}
function excludeNameReducer(name) {
return excludeReducer(function(item) {
return item.name === name;
});
}
var withoutFrank = array.reduce(excludeNameReducer("Frank"), []);
// excludes entity with name of Frank
var without12345 = array.reduce(excludeIdReducer("12345"), []);
// creates a new array excluding the entity with id 12345
// this is equivalent to the above
array.reduce(function(newArray, item) {
return (item.id === "12345") ? newArray : newArray.concat(item);
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment