Last active
May 26, 2019 10:13
-
-
Save jkasaudhan/a587460390a9723926e30a32f2e3b160 to your computer and use it in GitHub Desktop.
Array.flat() method in js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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