O(1)复杂度
```javascript
const data =[{"province":"上海市","city":"上海市市辖区","adcode":"310118","district":"青浦区"},
{"province":"江苏","city":"苏州市","adcode":"320506","district":"吴中区"},
{"province":"山西省","city":"阳泉市","adcode":"140302","district":"城区"},
{"province":"上海市","city":"上海市市辖区","adcode":"310101","district":"黄浦区"},
{"province":"河北省","city":"石家庄市","adcode":"130123","district":"正定县"}];
function listToTree(list) {
const map = {};
list.forEach(({province, city, district, adcode}) => {
const item = {name: district, value: adcode};
if (!map[province]) {
map[province] = {
name: province,
children: [{
name: city,
children: [item]
}]
}
return;
}
const cityItem = map[province].children.find(one =>
one.name === city);
if (!cityItem) {
map[province].children.push({ name: city,children: [item]})
} else {
cityItem.children.push(item);
}
})
return Object.values(map);
}
console.log(listToTree(data));
```