-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathflat.js
More file actions
34 lines (28 loc) · 739 Bytes
/
flat.js
File metadata and controls
34 lines (28 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Array.prototype.flat()
/**
* The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
* depth Optional -The depth level specifying how deep a nested array structure should be flattened. Defaults to 1.
*/
let numbers = [
1,
2,
[3, 4, 5],
[6, 7],
8,
9,
[
[10, 11],
[12, 13],
],
]
let arr = numbers.flat(2)
console.log(arr)
const arr1 = [0, 1, 2, [3, 4]]
console.log(arr1.flat())
// expected output: [0, 1, 2, 3, 4]
const arr2 = [0, 1, 2, [[[3, 4]]],[{4:{},5:{}}]]
console.log(arr2.flat(3))
// expected output: [0, 1, 2, [3, 4]]
const arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
arr4.flat(Infinity);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]