Skip to content

Instantly share code, notes, and snippets.

@jkasaudhan
Last active May 26, 2019 10:13
Show Gist options
  • Save jkasaudhan/a587460390a9723926e30a32f2e3b160 to your computer and use it in GitHub Desktop.
Save jkasaudhan/a587460390a9723926e30a32f2e3b160 to your computer and use it in GitHub Desktop.
Array.flat() method in js
var arr1 = [[1,2], [3,4], [5, 6]];
arr1.flat();
// [1,2,3,4,5,6]
var arr2 = [1, 2, [3, 4]];
arr2.flat();
// [1, 2, 3, 4]
var arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat();
// [1, 2, 3, 4, [5, 6]]
arr3.flat(2)
// [1,2,3,4,5,6]
// if you have unknown number of nested arrays, than you can use 'Infinity' as a depth parameter
arr3.flat(Infinity)
// [1,2,3,4,5,6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment