// 数据源
const arr = [{
id: '1',
children: [{
id: '1.1',
children: [{
id: '1.1.1',
}, {
id: '1.1.2',
}]
}]
}, {
id: '2',
children: [{
id: '2.1',
}, {
id: '2.2',
children: [{
id: '2.2.1',
}, {
id: '2.2.2',
}]
}]
}]
const expandKeys = ['2.2.1','1.1.1']
我要把命中 expandKeys
的 item
以及 父级item
的 expand
设置为 true
期望的结果
const arr = [{
id: '1',
expand: true,
children: [{
id: '1.1',
expand: true,
children: [{
id: '1.1.1',
expand: true
}, {
id: '1.1.2',
}]
}]
}, {
id: '2',
expand: true,
children: [{
id: '2.1',
}, {
id: '2.2',
expand: true,
children: [{
id: '2.2.1',
expand: true
}, {
id: '2.2.2',
}]
}]
}]
时间复杂度 最好 控制在 O(n^2)