树
const tree = [
{
name: '一级-parentA',
id: '0',
children: [
{
name: '二级-0-A',
id: '0-0',
},
{
name: '二级-1-A',
id: '0-1',
},
],
},
{
name: '一级-parentB',
id: '1',
children: [
{
name: '二级-0-B',
id: '1-0',
},
{
name: '二级-1-B',
id: '1-1',
},
],
},
];
搜索
treeData?.find((node) => {
if (node[name].includes(searchValue)) {
tempObj.found = node;
return node;
}
return treeSearch(node[key], searchValue, key, name, tempObj);
});
if (tempObj.found) {
return tempObj.found;
}
过滤
export function recursionTree(data, childrenKey, expresion) {
if (Array.isArray(data)) {
let result: any[] = [];
data.forEach((item) => {
let itemResult = recursionTree(item, childrenKey, expresion);
if (itemResult) {
result.push(itemResult);
}
});
return result.length > 0 ? result : null;
} else {
if (expresion(data)) {
if (data[childrenKey]) {
data[childrenKey] = recursionTree(
data[childrenKey],
childrenKey,
expresion
);
}
return data;
} else {
if (data[childrenKey]) {
data[childrenKey] = recursionTree(
data[childrenKey],
childrenKey,
expresion
);
return data[childrenKey] && data[childrenKey].length > 0 ? data : null;
} else {
return null;
}
}
}
}