// 假设后端返回的树形数据为 treeData
const treeData = [
{ id: 1, label: '节点A', children: [] },
{ id: 2, label: '节点B', children: [{ id: 3, label: '子节点C' }] },
// ...其他节点...
];
function fuzzySearch(treeData, keyword) {
const result = [];
function searchNode(node) {
if (node.label && node.label.includes(keyword)) {
result.push(node);
} else if (Array.isArray(node.children)) {
for (let i = 0; i < node.children.length; i++) {
searchNode(node.children[i]);
}
}
}
for (let i = 0; i < treeData.length; i++) {
searchNode(treeData[i]);
}
return result;
}
// 调用函数进行模糊搜索
const keyword = "节";
const results = fuzzySearch(treeData, keyword);
console.log(results);
后端返回树数据,针对label 进行模糊搜索 js
最新推荐文章于 2024-05-09 10:07:50 发布