vue 将一个多维数组转成一维数组

2022-09-01  本文已影响0人  二两毛豆
数据 不使用 ( flat() 方法时 )
禁用做记录(私)
data(){
    return{
        newList:[]
        list:[
            {
                id:1,
                name:一级,
                children:[
                    {
                        id:2,
                        name:二级,
                        children:[
                            {
                                id:3,
                                name:三级,
                                children:[]
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

methods:{
    async cutArr(){
        // await 即loop方法执行完才会将数据传回来,之后进行下一步操作
        this.newList= await this.loop(this.list)
        this.newList.forEach( item=>{
            item.children = []
        })
        console.log(this.newList)
    },
    loop(arr){
        arr.forEach( item=>{
            this.newList.push(item)
            // 如果当前条数据里面还有非空数组,则继续调loop方法遍历,直至结束
            if(item.children[0]){
                this.loop(item.children)
            }
        })
        return arr
    }
}

摘录:原文链接

上一篇 下一篇

猜你喜欢

热点阅读