SOURCE

let a = {
    provinces: [
        {
            code: '320000',
            cities: [
                {
                    code: '320100',
                    districts: [
                        {
                            code: '320102'
                        },
                        {
                            code: '320104'
                        }
                    ]
                },
                {
                    code: '320200'
                }
            ]
        },
        {
            code: '110000'
        },
        {
            code: '340000'
        }
    ]
}

const codes = [["320000", "320100", "320102"], ["320000", "320100", "320104"], ["320000", "320200"], ["110000"], ["340000"]]
const initMap = {};
// 递归生成省市区树形结构map
const getTreeMap = (map, level, code) => {
    if (level > 2) {
        return;
    }
    codes.forEach((item) => {
        // 第一级 或 上一级code等于当前code
        if (level === 0 || (level > 0 && item[level - 1] === code && item[level])) {
            map[item[level]] = {};
            if (item[level + 1]) {
                getTreeMap(map[item[level]], level + 1, item[level]);
            }
        }
    });
};
getTreeMap(initMap, 0)
const newMap = Object.keys(initMap).map(pk => {
    const province = {
        code: pk
    }
    const cities = Object.keys(initMap[pk])
    if (cities.length > 0) {
        province.cities = cities.map(ck => {
            const city = {
                code: ck
            }
            const districts = Object.keys(initMap[pk][ck])
            if (districts.length > 0) {
                city.districts = districts.map(d => ({ code: d }))
            }
            return city
        })
    }
    return province;
})

console.log(newMap)

const getTreeData = () => {
    const provinces = []
    codes.forEach(item => {
        item.forEach((e, index) => {
            const pCode = provinces.map(e => e.code);
            if (index == 0) {
                if (pCode.indexOf(e) == -1) {
                    const cItem = {
                        code: e,
                    }
                    if (item.length > 1) {
                        cItem.cities = []
                    }
                    provinces.push(cItem)
                }
            }

        })
    })
    provinces.forEach((pItem, pIndex) => {
        codes.forEach(item => {
            if (item[0] && item[1] && pItem.code === item[0]) {
                const cCode = provinces[pIndex].cities.map(e => e.code);
                if (cCode.indexOf(item[1]) == -1) {
                    const dItem = {
                        code: item[1]
                    }
                    if (item.length > 2) {
                        dItem.districts = []
                    }
                    provinces[pIndex].cities.push(dItem)
                }

            }
        })
    })
    provinces.forEach((pItem, pIndex) => {
        (pItem.cities || []).forEach((cItem, cIndex) => {
            codes.forEach(item => {
                if (item[1] && item[2] && cItem.code === item[1]) {
                    const dCode = provinces[pIndex].cities[cIndex].districts.map(e => e.code);
                    if (dCode.indexOf(item[2]) == -1) {
                        provinces[pIndex].cities[cIndex].districts.push({
                            code: item[2]
                        })
                    }
                }
            })
        })
    })
    return provinces;
}
getTreeData();



console 命令行工具 X clear

                    
>
console