希望明天越来越|

张尊娟

园龄:5年3个月粉丝:31关注:1

flat 拍平数组 手写flat拍平数组

// flat 拍平一维数组
       let flaoatArr = [1, 3, 5, 6, 3, 6, [3, 46, 465, 3]]
       let res = flat(flaoatArr)
       console.log(res);
       let res = flaoatArr.flat()
       console.log(res);
       
       
       
       // 手写float 数组  Array.prototype.concat.apply([], arr)
        let flaoatArr = [1, 3, 5, 6, 3, 6, [3, 46, 465, 3, [35, 4, 5, 3]]]
       function flat(arr) {
           let hasDeep = arr.some((item) => item instanceof Array)
           if (!hasDeep) {
               return arr
           }
           const res = Array.prototype.concat.apply([], arr)
           return flat(res)  //利用递归拍平多维数组
       }
       //第二种写法。本质都是一样
       function flat(arr){
           while(arr.some(item=>item instanceof Array)){
               arr = Array.prototype.concat.apply([],arr)
           }
           return arr
       }
       // concat 合并数组  可以合并
       let arr1 = [1,2,6,4]
       let arr2 = [5,4,6,4]
       let arr3 =arr1.concat(arr2)
       let arr3 =arr1.concat(9999,[111])
       let arr3 =arr1.concat(arr2,9999,[111])
       console.log(arr3);

        // arr.some()函数
       // some函数可以遍历每一项,都符合即返回true

本文作者:张尊娟

本文链接:https://www.cnblogs.com/wszzj/p/18003589

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   张尊娟  阅读(30)  评论(0编辑  收藏  举报
历史上的今天:
2021-02-02 商务楼宇小程序迭代更新复盘总结
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起