Skip to content

Latest commit

 

History

History
23 lines (22 loc) · 471 Bytes

0401.md

File metadata and controls

23 lines (22 loc) · 471 Bytes

实现一个flat()函数

Array.prototype.myFlat = function(num = 1) {
  if (Array.isArray(this)) {
    let arr = [];
    if (!Number(num) || Number(num) < 0) {
      return this;
    }
    this.forEach(item => {
      if(Array.isArray(item)){
        let count = num
        arr = arr.concat(item.myFlat(--count))
      } else {
        arr.push(item)
      }  
    });
    return arr;
  } else {
    throw this + ".flat is not a function";
  }
};