递归 

var arr = [1,2,[3,[4,5],6],7,[8],9]


    function handle(arr) {
      let res = []
      arr.forEach(item => {
        if(!isArr(item)) {
          res.push({ value: item })
        } else {
          res.push({ children: handle(item) })
        }
      }) 
      return res
    }

    function isArr(data) {
      return Array.isArray(data)
    }

    console.log(handle(arr))

map

function handle1(arr) {
      if(isArr(arr)) {
        return arr.map(item => handle1(arr))
      } else {
        return { value: arr }
      }
    }

 

 
posted on 2022-11-23 00:43  京鸿一瞥  阅读(13)  评论(0编辑  收藏  举报