SOURCE

var data = [
    {
        code: '01',
        data: [
            {
                code: '0101',
                data: [
                    {
                        code: '010101'
                    },
                    {
                        code: '010102'
                    },
                    
                ]
            },
            {
                code: '0102',
                data: [
                    {
                        code: '010201'
                    }
                ]
            }
        ]
    },
    {
        code: '02',
        data: [
            {
                code: '0201',
                data: [
                    {
                        code: '020101'
                    },
                    {
                        code: '020102'
                    }
                ]
            },
            {
                code: '0202',
                data: [
                    {
                        code: '020201'
                    }
                ]
            }
        ]
    }
]
let arr = []

let index = 0
data.forEach(item => {
    index++
    if(item.data) {
        item.data.forEach(item1 => {
            index++
            if(item1.data) {
                item1.data.forEach(item2 => {
                    index++
                    let obj = item2
                    obj.code1 = item.code
                    obj.code2 = item1.code
                    arr.push(obj)

                })
            }
        })
    }
})
console.log('循环次数=>', index)
console.log('处理后的arr=>', arr)
let arrIndex= 0
arr.forEach(arrItem => {
    arrIndex++
})
console.log('处理后arr循环次数=>', arrIndex)






console 命令行工具 X clear

                    
>
console