Skip to content

Instantly share code, notes, and snippets.

@NiallJoeMaher
Last active May 11, 2019 16:36
Show Gist options
  • Save NiallJoeMaher/f34e6a3c6db26b97d21f586018552c63 to your computer and use it in GitHub Desktop.
Save NiallJoeMaher/f34e6a3c6db26b97d21f586018552c63 to your computer and use it in GitHub Desktop.
const testArray1 = ["It's Always Sunny in", "", "Philadelphia"];
// First using a map for comparison
testArray1.map(x => x.split(" "));
//returns [["It's", "Always", "Sunny","in"],[""],["Philadelphia"]]
// Now let us see flatMap in action!
testArray1.flatMap(x => x.split(" "));
//returns ["It's","Sunny","in", "", "Philadelphia"]
// Now let's do some pointless math like all good tutorials should
const testArray2 = [1, 2, 3, 4, 5];
// First using a map for comparison
testArray2.map(num => [num, num * 2]);
// returns (5)[Array(2), Array(2), Array(2)];
// > 0: (2)[1, 2]
// > 1: (2)[2, 4]
// > 2: (2)[3, 6]
// > 3: (2)[4, 8]
// > 4: (2)[5, 10]
// Now let us see flatMap in action!
testArray2.flatMap(num => [num, num * 2]);
// returns [1, 2, 2, 4, 3, 6, 4, 8, 5, 10];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment