多维数组转换为一维数组

129 阅读1分钟
const nav= [  
    {    
        id: 1,    
        subitems: [      
           {       
                id: 2,
                subitems:[
                  {
                    id: 4,
                    subitems: []
          }        
        ]
      },
      {
        id: 3,
        subitems: []
      }
    ]
  }
]
function flatten(array) {  
return [].concat(...array.map(item => [].concat(item, ...flatten(item.subitems))))}

运用递归,将多维数组转换成一维数组