美文网首页
数组扁平化

数组扁平化

作者: TerdShow | 来源:发表于2019-07-24 15:48 被阅读0次
    function flatten(array) {
      return array.reduce((flat, current)=>{
        if(current instanceof Array){
          return flat.concat(flatten(current));
        } else{
          return [...flat, current];
        }
      },[])
    }
    
    let a = flatten([1,2,3,[4,5,[6,7],8],9]);
    console.log(a);
    

    原生:

    var newArray = arr.flat([depth]);
    

    相关文章

      网友评论

          本文标题:数组扁平化

          本文链接:https://www.haomeiwen.com/subject/xrnxrctx.html