-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflatArrToJSONTree.ts
52 lines (51 loc) · 1.27 KB
/
flatArrToJSONTree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
type JSONItem<T> = {
[P in keyof T]: T[P];
} & {
children?: T;
id: number;
parent_id: number;
};
const flatArrToJSONTree = <T, V>(
arr: Array<JSONItem<T>>,
rootId: V
): Array<any> => {
const result: Array<JSONItem<T>> = [];
// 建立 id - value 的映射
const map = arr.reduce((o, cur) => {
o[cur.id] = cur;
return o;
}, {});
for (let item of arr) {
if (item.parent_id === rootId) {
result.push(item);
}
if (item.parent_id in map) {
const parent = map[item.parent_id];
(parent.children || (parent.children = [])).push(item);
}
}
return result;
};
//test
let flatArr = [
{ id: 1, title: 'title1', parent_id: 0 },
{ id: 2, title: 'title2', parent_id: 0 },
{ id: 3, title: 'title2-1', parent_id: 2 },
{ id: 4, title: 'title3-1', parent_id: 3 },
{ id: 5, title: 'title4-1', parent_id: 4 },
{ id: 6, title: 'title3-2', parent_id: 3 },
];
console.log(flatArrToJSONTree(flatArr, 0));
/*
id: 1, title: 'title1', parent_id: 0 },
{ id: 2,
title: 'title2',
parent_id: 0,
children:
[ { id: 3,
title: 'title2-1',
parent_id: 2,
children:
[ { id: 4, title: 'title3-1', parent_id: 3, children: [Object] },
{ id: 6, title: 'title3-2', parent_id: 3 } ] } ] } ]
*/