Skip to content

Instantly share code, notes, and snippets.

@himynameisdave
Last active May 21, 2017 21:26
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 himynameisdave/f86bcb9b80d519a3a8815fef6dd2bed8 to your computer and use it in GitHub Desktop.
Save himynameisdave/f86bcb9b80d519a3a8815fef6dd2bed8 to your computer and use it in GitHub Desktop.
Reduce your fears about .reduce() - Array.find with reduce 03
// 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