Skip to content

Commit 6b72559

Browse files
committed
feat: 新增数组转树
1 parent 2c9a188 commit 6b72559

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// vue3 的数据劫持主要是使用proxy实现的
2+
3+
const rawObject = {
4+
name: 'coderwei',
5+
age: 18
6+
}
7+
8+
const effect = () => {
9+
console.log('渲染dom');
10+
}
11+
12+
let set = new Set()
13+
14+
const trick = (target, key) => {
15+
set.add(effect)
16+
}
17+
const tigger = (target, key) => {
18+
for (const fn of set) {
19+
fn()
20+
}
21+
}
22+
23+
const optionFunction = {
24+
get(target, key, value) {
25+
// 收集依赖
26+
trick(target, key)
27+
console.log(target, key, value);
28+
},
29+
set(target, key, value) {
30+
// 触发依赖
31+
tigger()
32+
console.log(target, key, value);
33+
},
34+
deleteProperty() {
35+
console.log('delete操作');
36+
}
37+
}
38+
39+
const reactive = (obj) => {
40+
return new Proxy(obj, optionFunction)
41+
}
42+
43+
const obj = reactive(rawObject)
44+
// console.log(obj);
45+
46+
delete obj.name
+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
let originalObj = [
2+
{
3+
id: 1,
4+
name: "键盘专区",
5+
path: "xxx/xxx/xxx.vue",
6+
pid: 0,
7+
icon: "xxxx",
8+
},
9+
{
10+
id: 2,
11+
name: "鼠标专区",
12+
path: "xxx/xxx/xxx.vue",
13+
pid: 0,
14+
icon: "xxxx",
15+
},
16+
{
17+
id: 4,
18+
name: "显卡专区",
19+
path: "xxx/xxx/xxx.vue",
20+
pid: 0,
21+
icon: "xxxx",
22+
},
23+
{
24+
id: 5,
25+
name: "达尔优键盘",
26+
path: "xxx/xxx/xxx.vue",
27+
pid: 1,
28+
icon: "xxxx",
29+
},
30+
{
31+
id: 6,
32+
name: "腹灵cmk87键盘",
33+
path: "xxx/xxx/xxx.vue",
34+
pid: 1,
35+
icon: "xxxx",
36+
},
37+
{
38+
id: 7,
39+
name: "七彩虹gtx 1660ti",
40+
path: "xxx/xxx/xxx.vue",
41+
pid: 4,
42+
icon: "xxxx",
43+
},
44+
{
45+
id: 8,
46+
name: "华硕3050ti",
47+
path: "xxx/xxx/xxx.vue",
48+
pid: 4,
49+
icon: "xxxx",
50+
},
51+
{
52+
id: 9,
53+
name: "微星 4090ti",
54+
path: "xxx/xxx/xxx.vue",
55+
pid: 4,
56+
icon: "xxxx",
57+
},
58+
{
59+
id: 10,
60+
name: "g502",
61+
path: "xxx/xxx/xxx.vue",
62+
pid: 2,
63+
icon: "xxxx",
64+
},
65+
{
66+
id: 11,
67+
name: "毒奎v2",
68+
path: "xxx/xxx/xxx.vue",
69+
pid: 2,
70+
icon: "xxxx",
71+
},
72+
{
73+
id: 12,
74+
name: "雷蛇v3",
75+
path: "xxx/xxx/xxx.vue",
76+
pid: 2,
77+
icon: "xxxx",
78+
},
79+
{
80+
id: 13,
81+
name: "雷蛇v3 children",
82+
path: "xxx/xxx/xxx.vue",
83+
pid: 12,
84+
icon: "xxxx",
85+
},
86+
{
87+
id: 14,
88+
name: "达尔优 children",
89+
path: "xxx/xxx/xxx.vue",
90+
pid: 5,
91+
icon: "xxxx",
92+
},
93+
{
94+
id: 15,
95+
name: "达尔优 children children",
96+
path: "xxx/xxx/xxx.vue",
97+
pid: 14,
98+
icon: "xxxx",
99+
},
100+
];
101+
102+
// 实现一个方法 将上述数组转成树状结构
103+
104+
// 1. 递归
105+
// 处理parentData 和childrenData 之间关系的函数
106+
function parseChildrenToParent(parentData, childrenData) {
107+
childrenData.forEach(item => {
108+
const tempData = parentData.find(_item => _item.id == item.pid);
109+
if (!tempData) return;
110+
tempData.children = tempData.children ?? [];
111+
tempData.children.push(item);
112+
// if (tempData.children) {
113+
// // 有值 递归
114+
// tempData.children.push(item);
115+
// } else {
116+
// tempData.children = [item];
117+
// }
118+
parseChildrenToParent([item], childrenData);
119+
});
120+
}
121+
122+
function arrayToTree(arr) {
123+
let tree = [];
124+
125+
function findParent(arr, pid = 0) {
126+
const parentData = arr.filter(item => item.pid == 0),
127+
childrenData = arr.filter(item => item.pid != 0);
128+
parseChildrenToParent(parentData, childrenData);
129+
tree = parentData;
130+
}
131+
132+
findParent(arr);
133+
return tree;
134+
}
135+
136+
console.log(arrayToTree(originalObj));

0 commit comments

Comments
 (0)