-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path递归重构数据.html
125 lines (91 loc) · 4.5 KB
/
递归重构数据.html
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
let arr = [
{ comment_id: 1, user_id: 653, comment_date: "05-20", comment_content: "加勒比很好看", parent_id: null }, // 一级评论
{ comment_id: 2, user_id: 172, comment_date: "05-20", comment_content: "还不错哦!很好看", parent_id: null },// 一级评论
{ comment_id: 3, user_id: 1193, comment_date: "05-22", comment_content: "我也觉得加勒比很好看", parent_id: "1" },// 二级评论 , 回复一级评论
{ comment_id: 4, user_id: 1238, comment_date: "05-22", comment_content: "我感觉霍比特人更好看一点", parent_id: "3" },// 三级评论, 回复二级评论
{ comment_id: 5, user_id: 1238, comment_date: "05-23", comment_content: "杰克船长,好帅", parent_id: null },// 一级评论
{ comment_id: 6, user_id: 744, comment_date: "05-23", comment_content: "我觉得都很好看", parent_id: "4" },// 四级评论 , 回复三级评论
{ comment_id: 7, user_id: 744, comment_date: "05-23", comment_content: "你是个白嫖怪", parent_id: "6" },// 五级评论 , 回复四级评论
{ comment_id: 8, user_id: 744, comment_date: "05-23", comment_content: "我也觉得杰克船长很帅", parent_id: "5" },// 二级评论 , 回复一级评论
{ comment_id: 9, user_id: 1745, comment_date: "06-01", comment_content: "两个都帅哈哈哈~~~", parent_id: "8" }// 三级评论 ,回复二级评论
]
// 改造后的数据: 一级里面放,二级; 二级里放三级; 三级里面放四级.....
// [
// { comment_id: 1, user_id: 653, comment_date: "05-20", comment_content: "加勒比很好看", parent_id: null ,child:[ // 一级评论
// { comment_id: 3, user_id: 1193, comment_date: "05-22", comment_content: "我也觉得加勒比很好看", parent_id: "1" ,clild:[// 二级评论 , 回复一级评论
// { comment_id: 4, user_id: 1238, comment_date: "05-22", comment_content: "我感觉霍比特人更好看一点", parent_id: "3",child:[// 三级评论, 回复二级评论
// { comment_id: 6, user_id: 744, comment_date: "05-23", comment_content: "我觉得都很好看", parent_id: "4",child:[// 四级评论 , 回复三级评论
// { comment_id: 7, user_id: 744, comment_date: "05-23", comment_content: "你是个白嫖怪", parent_id: "6" },// 五级评论 , 回复四级评论
// ] },
// ] },
// ] },
// ]},
// ]
// 方法一: for循环
/* let arr1 = [];
for(var i = 0; i < arr.length; i++){
if(arr[i].parent_id == null){// 找出所有 一级评论
arr1.push(arr[i])
for(var j = 0; j < arr.length; j++){// 找出二级评论, 并且存放在一级评论对象 的child数组中
if(arr[i].comment_id == arr[j].parent_id){// 如果一级的评论id == 二级的回复id
arr[i].child = [arr[j]]// 就把 二级评论, 存放到一级的child数组里
for(var k = 0 ; k < arr.length; k++){// 找出三级评论, 并且存放在二级评论对象 的child数组中
if(arr[j].comment_id == arr[k].parent_id){
arr[j].child = [arr[k]]
}
}
}
}
}
}
console.log(arr1);
*/
// 方法二: 递归
function fn(temp) {
let arr1 = []
for(var i = 0; i < arr.length; i++){
if(arr[i].parent_id == temp){// 如果数据id 和 评论id 一样
arr1.push(arr[i])// 就把当前的数据 都 push到 child数组中
arr[i].child = fn(arr[i].comment_id)// 自调用 每次传当前的评论id
}
}
return arr1
}
const res = fn(null)
console.log(res);
// 一张纸厚度为0.0001米, 折叠多少次可以超过10000米
// let a = 0.0001;
// let num = 0;
// for(i=0;;i++){
// a *= 2
// num++;
// if(a > 10000){
// break;
// }
// }
// console.log(num);
// 27 次
// let a = 0.0001;
// let num = 0;
// function fn(){
// a *= 2 // a += a ,折叠一次
// num ++
// if(a < 10000){
// fn()
// }
// console.log(num);
// }
// fn()
// 27 次
</script>
</html>