Skip to main content

实现Array.prototype.flat()

MDN-Array/flat

实现Array.prototype.flat()

📢 参数 深度, 默认为1

Array.prototype.myFlat = function (depth = 1) {
if (!Number(depth) || Number(depth) < 0) {
return this;
}
let res = this.concat();
while (depth > 0) {
if (!res.some(v => Array.isArray(v))) {
break;
}
// res = [].concat.apply([], res);
res = [].concat(...res)
depth--;
}
return res;
}