Last active
May 21, 2017 21:26
-
-
Save himynameisdave/f86bcb9b80d519a3a8815fef6dd2bed8 to your computer and use it in GitHub Desktop.
Reduce your fears about .reduce() - Array.find with reduce 03
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
// arrayFind accepts an array and returns a function | |
// the returned function accepts the finder function | |
const arrayFind = arr => fn => arr.reduce((acc, item, index) => { | |
// We pass the finder function the item and the index | |
if (fn(item, index)) return item; | |
return acc; | |
}); | |
// Creates a finder function for just our fruits | |
const fruitFinder = arrayFind(fruits); | |
// Now we can pass a finder function to fruitFinder (seen as `fn` above) | |
const thisShitIsBananas = fruitFinder(fruit => fruit.name === 'bananas'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment