From a95cd97cf55a11e1bda53d1235b1622f63e7c442 Mon Sep 17 00:00:00 2001 From: linxianxi <904492381@qq.com> Date: Fri, 4 Aug 2023 17:19:58 +0800 Subject: [PATCH] perf: map filter -> reduce --- src/hooks/useFilterTreeData.ts | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/hooks/useFilterTreeData.ts b/src/hooks/useFilterTreeData.ts index 1433fa83..ec5b195b 100644 --- a/src/hooks/useFilterTreeData.ts +++ b/src/hooks/useFilterTreeData.ts @@ -38,23 +38,21 @@ export default ( } function dig(list: DefaultOptionType[], keepAll: boolean = false) { - return list - .map(dataNode => { - const children = dataNode[fieldChildren]; - - const match = keepAll || filterOptionFunc(searchValue, fillLegacyProps(dataNode)); - const childList = dig(children || [], match); - - if (match || childList.length) { - return { - ...dataNode, - isLeaf: undefined, - [fieldChildren]: childList, - }; - } - return null; - }) - .filter(node => node); + return list.reduce((total, dataNode) => { + const children = dataNode[fieldChildren]; + + const match = keepAll || filterOptionFunc(searchValue, fillLegacyProps(dataNode)); + const childList = dig(children || [], match); + + if (match || childList.length) { + total.push({ + ...dataNode, + isLeaf: undefined, + [fieldChildren]: childList, + }); + } + return total; + }, []); } return dig(treeData);