( resource: T[], id: string, parentId: string, children: string, )
| 34 | * @author: 白雾茫茫丶 |
| 35 | */ |
| 36 | export 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: 将树形数据转成层级对象,主要是国际化数据 |
no outgoing calls
no test coverage detected