Skip to content

Commit

Permalink
fix(TreeData): fromList 算法优化
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 13, 2021
1 parent 82dc582 commit d911d74
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions src/utils/TreeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,31 +615,22 @@ export class TreeData<TNode extends TreeDataNode> {
list: TItem[],
idKey: keyof TItem,
parentIdKey: keyof TItem,
cloneIgnore?: TreeDataOptions<TItem>['cloneIgnore'],
): TreeData<TreeDataStandardNode<TItem>> {
const _list: Array<TreeDataStandardNode<TItem>> = cloneDeepFast(
list,
cloneIgnore,
) as any
const IS_CHILD_KEY = '__tree_data_is_child__'
const data = _list
.map(item => {
item.children = _list.filter(item2 => {
if (item2[IS_CHILD_KEY] === true) return false
const isChild =
(item2 as any)[parentIdKey] != null &&
(item as any)[idKey] === (item2 as any)[parentIdKey]
if (isChild) {
Object.defineProperty(item2, IS_CHILD_KEY, {
value: true,
enumerable: false,
})
}
return isChild
}) as any
return item
})
.filter(item => !item[IS_CHILD_KEY])
return new TreeData(data)
const itemMap: Record<any, TItem> = {}
for (let i = 0; i < list.length; i++) {
const item = list[i] as any
item.children = []
itemMap[item[idKey]] = item
}
const tree: TItem[] = []
for (let i = 0; i < list.length; i++) {
const item = list[i] as any
if (itemMap[item[parentIdKey]] !== undefined) {
itemMap[item[parentIdKey]].children.push(item)
} else {
tree.push(item)
}
}
return new TreeData(tree as any)
}
}

0 comments on commit d911d74

Please sign in to comment.