forked from zzzzzhowie/practical-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat.js
More file actions
23 lines (21 loc) · 716 Bytes
/
flat.js
File metadata and controls
23 lines (21 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// reduce实现 Array.prototype.flat,数组扁平化
const selfFlat = function (depth = 1) {
let arr = Array.prototype.slice.call(this)
if(depth === 0 ) return arr
return arr.reduce((pre, cur) => {
if (Array.isArray(cur)) {
// 需要用 call 绑定 this 值,否则会指向 window
return [...pre, ...selfFlat.call(cur,depth-1)]
} else {
return [...pre, cur]
}
}, [])
}
Array.prototype.selfFlat || (Object.defineProperty(Array.prototype, 'selfFlat', {
value: selfFlat,
enumerable: false,
configurable: true,
writable: true
}))
let arr = [1, 2, [3, 4, [5, 6, 7, 8], 9], 10, 11, 12, [13, 14]]
console.log(arr.selfFlat())