Skip to content

Instantly share code, notes, and snippets.

@webber2408
Created July 6, 2020 15:22
Show Gist options
  • Save webber2408/1f5350e41528ee8d9d187519d8999a9e to your computer and use it in GitHub Desktop.
Save webber2408/1f5350e41528ee8d9d187519d8999a9e to your computer and use it in GitHub Desktop.
Array Flat (array.flat())
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