tree型数据
data = [
{
id: 1,
label: '江苏',
code: '邮政编码-江苏',
children: [
{
id: 1-1,
label: '南京',
code: '邮政编码-南京',
children: [
{
id: 1-1,
label: '栖霞',
code: '邮政编码-栖霞',
children: [ ... ] / [],
},
],
},
],
},
{
id: 2,
label: '陕西',
code: '邮政编码-陕西',
children: [
{
id: 2-1,
label: '西安',
code: '邮政编码-西安',
children: [
{
id: 2-1-1,
label: '西安的某个区',
code: '邮政编码-西安的某个区',
children: [ ... ] / [],
},
],
},
],
}
]
1、递归查询
export const isBlank = (val) => {
if(typeof val === 'number') {
return false
}else if(typeof val === 'string' && isOnlyWhitespace(val)){
return true
}else {
return _.isEmpty(val) // lodash的判断引用数据类型、boolean是否为空的方法
}
}
export const recursiveFindLabel = (list, id) => {
for (const item of list) {
if (item.id === id) {
return item.label
}
if (item.children && item.children.length > 0) {
const child = recursiveFindLabel(item.children, id)
if (!_.isEmpty(child)) {
return child
}
}
}
return null
}