Skip to content

Instantly share code, notes, and snippets.

@munkacsitomi
Last active January 3, 2019 08:30
Show Gist options
  • Select an option

  • Save munkacsitomi/a79ea3af0eacaa5ebf2e3ff58a5ba5e7 to your computer and use it in GitHub Desktop.

Select an option

Save munkacsitomi/a79ea3af0eacaa5ebf2e3ff58a5ba5e7 to your computer and use it in GitHub Desktop.
const kittens = animals.find(x => x.type === 'cat' && x.ageMonths < 12);
const kittens = animals.find(animal => animal.type === 'cat' && animal.ageMonths < 12);
const kittens = animals.filter(animal => animal.type === 'cat').filter(cat => cat.ageMonths < 12);
const kittens = animals.filter(onlyKittens);
const onlyKittens = ({ type, ageMonths }) => type === 'cat' && ageMonths < 12;
// When to use map?
// .map() when you want to transform elements in an array.
// When to use filter?
// .filter() when you want to select a subset of multiple elements from an array.
// When to use find?
// .find() When you want to select a single element from an array.
// When to use reduce?
// .reduce() when you want derive a single value from multiple elements in an array.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment