Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 457 Bytes

多维数组降为一维数组.md

File metadata and controls

32 lines (23 loc) · 457 Bytes

请将多维数组转为一维数组

示例:

输入: [1, [2, [3, [4, 5]]],[6,7,[8,9]]]

输出: [1,2,3,4,5,6,7,8,9]

你的答案:

递归实现:

let newArr = [];

const descending = (arr) => {
  if(arr === null){
    return arr;
  }

  arr.forEach((item)=>{
    if(Array.isArray(item)){
      descending(item);
    }else{
      newArr.push(item);
    }
  })
}

descending(arr);
console.log(newArr); //[1,2,3,4,5,6,7,8,9]