const arr = [
{ province: '广东省', city: '广州市', district: '天河区' },
{ province: '广东省', city: '广州市', district: '白云区' },
{ province: '广东省', city: '东莞市', district: '常平镇' },
]
const newArr = []
arr.forEach(info => {
const newObj = {}
newObj.name = info.province
newObj.children = []
if (!newArr.find(info1 => info1.name === info.province)) {
newArr.push(newObj)
}
arr.forEach(info => {
const newObj1 = {}
newObj1.name = info.city
newObj1.children = []
if (
!newObj.children.find(info2 => info2.name === info.city) &&
newObj.name === info.province
) {
newObj.children.push(newObj1)
}
arr.forEach(info => {
const newObj2 = {}
newObj2.name = info.district
if (
!newObj1.children.find(info3 => info3.name === info.district) &&
newObj1.name === info.city
) {
newObj1.children.push(newObj2)
}
})
})
})
console.log(newArr)
数组对象一级地址 处理成三级联动
最新推荐文章于 2022-06-17 21:56:23 发布