From ddd940887866d07122bf9859049570141b93a790 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E4=BB=99=E9=83=BD=E6=B2=A1=E7=94=A8?= <615206459@qq.com> Date: Mon, 9 Oct 2023 19:45:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cool/utils/index.ts | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/cool/utils/index.ts b/src/cool/utils/index.ts index f0e5923..e550c59 100644 --- a/src/cool/utils/index.ts +++ b/src/cool/utils/index.ts @@ -200,34 +200,26 @@ export function deepPaths(paths: string[], splitor?: string) { } // 列表转树形 -export function deepTree(list: any[], order?: "asc" | "desc"): any[] { +export function deepTree(list: any[], sort?: "desc" | "asc"): any[] { const newList: any[] = []; const map: any = {}; - list.forEach((e) => (map[e.id] = e)); + orderBy(list, "orderNum", sort) + .map((e) => { + map[e.id] = e; + return e; + }) + .forEach((e) => { + const parent = map[e.parentId]; - list.forEach((e) => { - const parent = map[e.parentId]; - - if (parent) { - (parent.children || (parent.children = [])).push(e); - } else { - newList.push(e); - } - }); - - const fn = (list: Array) => { - list.map((e) => { - if (isArray(e.children)) { - e.children = orderBy(e.children, "orderNum", order); - fn(e.children); + if (parent) { + (parent.children || (parent.children = [])).push(e); + } else { + newList.push(e); } }); - }; - fn(newList); - - return orderBy(newList, "orderNum", order); + return newList; } // 树形转列表