let cityList = [
{id: 1, parentId: 0, name:'江苏省'},
{id: 2, parentId: 0, name:'广东省'},
{id: 3, parentId: 0, name:'安徽省'},
{id: 4, parentId: 1, name:'苏州市'},
{id: 5, parentId: 1, name:'无锡市'},
{id: 6, parentId: 1, name:'南京市'},
{id: 7, parentId: 2, name:'广州市'},
{id: 8, parentId: 2, name:'深圳市'},
{id: 9, parentId: 3, name:'合肥市'},
{id: 10, parentId: 4, name:'工业园区'},
{id: 11, parentId: 4, name:'吴中区'},
{id: 12, parentId: 4, name:'姑苏区'},
{id: 13, parentId: 9, name:'肥东区'},
{id: 14, parentId: 9, name:'肥西区'},
{id: 15, parentId: 6, name:'江宁区'},
{id: 16, parentId: 6, name:'玄武区'}
];
let treeArr = [];
cityList.forEach(item => {
if(item.parentId === 0){
treeArr.push(item);
}
// 每一项都添加一个children
item.children = cityList.filter(child => child.parentId === item.id);
});
console.log(treeArr);