Skip to content

Instantly share code, notes, and snippets.

@jthoms1
Created November 10, 2016 00:23
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 jthoms1/fa8c5fcae5f3deb7a277db84980eb6c0 to your computer and use it in GitHub Desktop.
Save jthoms1/fa8c5fcae5f3deb7a277db84980eb6c0 to your computer and use it in GitHub Desktop.
array chaining
let things = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let other = things
.map(function(num) { return num + 1; }) // creates a new array that contains [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] and returns it
.filter(function(num) { return num % 3 === 0; }) // reduces the array contents so that it contains [3, 6, 9] and returns it
.join(' -> '); // joins the values and returns a string '3 -> 6 -> 9'
// This could just as easily be written as follows
let a = things.map(function(num) { return num + 1; });
let b = a.filter(function(num) { return num % 3 === 0; });
let c = b.join(' -> ');
// But this means that we have extra variables laying around that we may not use. If you write
// them as simple chained manipulations we know that the intermediate values are not important and ar thrown away.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment