const options = [{
firstvalue: 'zhejiang',
firstlabel: 'Zhejiang',
children: [{
secondvalue: 'hangzhou',
secondlabel: 'Hangzhou',
children: [{
thirdvalue: 'xihu',
thirdlabel: 'West Lake',
}],
}],
},
{
firstvalue: 'jiangsu',
firstlabel: 'Jiangsu',
children: [{
secondvalue: 'nanjing',
secondlabel: 'Nanjing',
children: [{
thirdvalue: 'zhonghuamen',
thirdlabel: 'Zhong Hua Men',
}],
}],
}
]
var exitNo = ['firstvalue', 'firstlabel', 'secondvalue', 'secondlabel', 'thirdvalue', 'thirdlabel']
function seachSort(tree, temp = []) {
for (let key of tree) {
for (let node in key) {
if (exitNo.includes(node)) {
if (node.includes('value')) {
key['value'] = key[node]
delete key[node];
} else if (node.includes('label')) {
key['label'] = key[node]
delete key[node];
}
} else {
if (key['children'] && key['children'].length > 0) {
seachSort(key['children'])
// seachSort(key['children'])
}
}
}
}
return tree
}
console.log(seachSort(options))
05-25
1337
06-20
7626
09-28
3646