Created
July 6, 2020 15:22
-
-
Save webber2408/1f5350e41528ee8d9d187519d8999a9e to your computer and use it in GitHub Desktop.
Array Flat (array.flat())
This file contains 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]]; | |
console.log(arr1.flat()); | |
// [1, 2, 3, 4] | |
var arr2 = [1, 2, [3, 4, [5, 6]]]; | |
console.log(arr2.flat()); // Default Level : 1 | |
// [1, 2, 3, 4, [5, 6]] | |
var arr3 = [1, 2, [3, 4, [5, 6]]]; | |
console.log(arr3.flat(2)); // Explicitly specified level : 2 | |
// [1, 2, 3, 4, 5, 6] | |
var arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]]; | |
console.log(arr4.flat(Infinity)); | |
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
var arr5 = [1, 2, , 4, 5]; | |
console.log(arr5.flat()); | |
// [1, 2, 4, 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment