react ant design Cascader多选数据回显
const test = [
{
id: 2,
children: [
{
id: 5,
children: [
{
id: 3,
},
{
id: 4,
children: [
{
id: 7,
},
{
id: 8,
},
],
},
],
},
],
},
];
const getHierarchyData = (rawData, list) => {
const data = [];
const find = (classifyList, id) => {
let temp = [id];
let forFn = function (arr, id) {
for (let i = 0; i < arr.length; i++) {
let item = arr[i];
if (item.children) {
let data1= item.children.find((item1) => item1.id === id);
if (data1) {
temp.unshift(item.id);
forFn(classifyList, item.id);
break;
} else {
forFn(item.children, id);
}
}
}
};
forFn(classifyList, id);
return temp;
};
list.forEach((str) => {
data.push(find(rawData, str));
});
return data;
};
console.log(getHierarchyData(test , [8,3]))
结果:[[2, 5, 4, 8],[2, 5, 3]]