MCPcopy Create free account
hub / github.com/baiwumm/react-admin / initializeTree

Function initializeTree

Xmw_server/src/utils/index.ts:36–55  ·  view source on GitHub ↗
(
  resource: T[],
  id: string,
  parentId: string,
  children: string,
)

Source from the content-addressed store, hash-verified

34 * @author: 白雾茫茫丶
35 */
36export function initializeTree<T>(
37 resource: T[],
38 id: string,
39 parentId: string,
40 children: string,
41): T[] {
42 const temp = JSON.parse(JSON.stringify(resource));
43 // 以id为键,当前对象为值,存入一个新的对象中
44 const tempObj = {};
45 for (const i in temp) {
46 tempObj[temp[i][id]] = temp[i];
47 }
48 return temp.filter((father: T) => {
49 // 把当前节点的所有子节点找到
50 const childArr = temp.filter((child: T) => father[id] == child[parentId]);
51 childArr.length > 0 ? (father[children] = childArr) : '';
52 // 只返回第一级数据;如果当前节点的fatherId不为空,但是在父节点不存在,也为一级数据
53 return father[parentId] === null || !tempObj[father[parentId]];
54 });
55}
56
57/**
58 * @description: 将树形数据转成层级对象,主要是国际化数据

Callers 6

getRoutesMenusMethod · 0.90
getJobsListMethod · 0.90
getOrganizationListMethod · 0.90
getAllLocalesLangMethod · 0.90
getInternationalListMethod · 0.90
getMenuListMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected