-
Notifications
You must be signed in to change notification settings - Fork 181
/
3.flat.js
58 lines (50 loc) · 915 Bytes
/
3.flat.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
*
* @param {*} array 深层嵌套的数据
* @returns array 新数组
*/
const flat1 = (array) => {
return array.reduce((result, it) => {
return result.concat(Array.isArray(it) ? flat1(it) : it)
}, [])
}
let arr1 = [
1,
[ 2, 3, 4 ],
[ 5, [ 6, [ 7, [ 8 ] ] ] ]
]
console.log(flat1(arr1))
// js原生的flat方法
/**
*
* @param {*} array 深层嵌套的数据
* @returns 新数组
*/
const flat2 = (array) => {
return array.flat(Infinity)
}
let arr2 = [
1,
[ 2, 3, 4 ],
[ 5, [ 6, [ 7, [ 8 ] ] ] ]
]
console.log(flat2(arr2))
const flat3 = (array) => {
const result = []
const stack = [ ...array ]
while (stack.length !== 0) {
const val = stack.pop()
if (Array.isArray(val)) {
stack.push(...val)
} else {
result.unshift(val)
}
}
return result
}
let arr3 = [
1,
[ 2, 3, 4 ],
[ 5, [ 6, [ 7, [ 8 ] ] ] ]
]
console.log(flat3(arr3))