Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jkasaudhan/f6393cdab80535381c1802c429874e08 to your computer and use it in GitHub Desktop.
Save jkasaudhan/f6393cdab80535381c1802c429874e08 to your computer and use it in GitHub Desktop.
Array flat map method
// Without .flatMap()
var arr1 = [1, 2, 3, 4];
arr1.map(x => [x * 2]);
// [[2], [4], [6], [8]]
arr1.map(x => [x * 2]).flat();
// [2,4,6,8]
// With .flatMap() => it is faster than above method
arr1.flatMap(x => [x * 2]);
// [2, 4, 6, 8]
// only one level is flattened
arr1.flatMap(x => [[x * 2]]);
// [[2], [4], [6], [8]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment