Created
January 7, 2014 13:21
-
-
Save krzysztofantczak/8299186 to your computer and use it in GitHub Desktop.
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
// 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