Skip to content

Commit 03f3b24

Browse files
authored
perf: map filter -> reduce (#438)
1 parent ac7205f commit 03f3b24

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/hooks/useFilterTreeData.ts

+15-17
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,21 @@ export default (
3838
}
3939

4040
function dig(list: DefaultOptionType[], keepAll: boolean = false) {
41-
return list
42-
.map(dataNode => {
43-
const children = dataNode[fieldChildren];
44-
45-
const match = keepAll || filterOptionFunc(searchValue, fillLegacyProps(dataNode));
46-
const childList = dig(children || [], match);
47-
48-
if (match || childList.length) {
49-
return {
50-
...dataNode,
51-
isLeaf: undefined,
52-
[fieldChildren]: childList,
53-
};
54-
}
55-
return null;
56-
})
57-
.filter(node => node);
41+
return list.reduce((total, dataNode) => {
42+
const children = dataNode[fieldChildren];
43+
44+
const match = keepAll || filterOptionFunc(searchValue, fillLegacyProps(dataNode));
45+
const childList = dig(children || [], match);
46+
47+
if (match || childList.length) {
48+
total.push({
49+
...dataNode,
50+
isLeaf: undefined,
51+
[fieldChildren]: childList,
52+
});
53+
}
54+
return total;
55+
}, []);
5856
}
5957

6058
return dig(treeData);

0 commit comments

Comments
 (0)