多维数组转化为一维数组

判断数组的类型为数组,而不是Object

   arr.constructor == Array
   instanceof  arr == Array 
   Array.isArray()

reduce

第一个参数是function,第二个参数可以是数组,可以是数字,
reduce(function(only,item,index,arr){
  
},[])

递归

   var arr = [1,2,3,[4,5,6,[7,8]]]
    let newArr = [];

    function mergeArr(arr){
        arr.forEach(item => {
            item.constructor == Array?mergeArr(item):newArr.push(item)
        });
        return newArr
    }

数组的方法

    arr.flat()   //只可以转化二维数组
posted @ 2021-03-11 11:25  ~~Distance  阅读(89)  评论(0编辑  收藏  举报