const convert = data => {
const options = []
const proviceMap = {}
data.forEach(item => {
// 没有省
if (!proviceMap[item.province]) {
proviceMap[item.province] = {
index: options.length,
citys: [item.city]
}
options.push({
value: item.province,
label: item.province,
children: [
{
value: item.city,
label: item.city,
children: [
{
value: item.adcode,
label: item.district,
}
]
}
]
})
return
}
const provinceIndex = proviceMap[item.province].index
const cityIndex = proviceMap[item.province].citys.indexOf(item.city)
// 没有市
if (cityIndex === -1) {
options[provinceIndex].children.push({
value: item.city,
label: item.city,
children: [
{
value: item.adcode,
label: item.district,
}
]
})
return
}
// 有省有市
options[provinceIndex].children[cityIndex].children.push({
value: item.adcode,
label: item.district,
})
})
}