Skip to content

Instantly share code, notes, and snippets.

@chriswrightdesign
Last active June 23, 2017 06:43
Show Gist options
  • Save chriswrightdesign/d5e229c50871f314dac736bfa5f0d8f6 to your computer and use it in GitHub Desktop.
Save chriswrightdesign/d5e229c50871f314dac736bfa5f0d8f6 to your computer and use it in GitHub Desktop.
Passing functions to map
const myAnimals = ['dog', undefined, 'cat', 'bird', undefined, 0, false];
myAnimals.filter(Boolean); // Result: ['dog', 'cat', 'bird'];
/*
it will call Boolean('dog'), then Boolean(undefined),
then Boolean('cat') etc etc, the array.filter() callback must return true to keep
an element in the array, if its false it excludes it
*/
const myValues = [1.34, 3.5, 3.6, 4.1];
// now using Math.round() on .map()
myValues.map(Math.round) // [1, 4, 4, 4];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment