-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
81 lines (78 loc) · 2.42 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
MongoDb recursividad
</body>
<script>
let categories = [
{
"_id": "5c9d82efb606851550bc83dd",
"name": "Tech",
"createdAt": "2019-03-29T02:29:03.152Z",
"updatedAt": "2019-03-29T02:29:03.152Z",
"__v": 0
},
{
"_id": "5c9d8301b606851550bc83df",
"name": "Computadoras",
"parent_key": "5c9d82efb606851550bc83dd",
"createdAt": "2019-03-29T02:29:21.294Z",
"updatedAt": "2019-03-29T02:29:21.294Z",
"__v": 0
},
{
"_id": "5c9d8316b606851550bc83e1",
"name": "Discos duros",
"parent_key": "5c9d8301b606851550bc83df",
"createdAt": "2019-03-29T02:29:42.154Z",
"updatedAt": "2019-03-29T02:29:42.154Z",
"__v": 0
},
{
"_id": "5c9d84884bda6b07f0cbe82b",
"name": "Discos duros 2",
"parent_key": "5c9d8316b606851550bc83e1",
"createdAt": "2019-03-29T02:35:52.056Z",
"updatedAt": "2019-03-29T02:35:52.056Z",
"__v": 0
},
{
"_id": "5c9da323218b1b0c7457cd4f",
"name": "Ropa",
"createdAt": "2019-03-29T04:46:27.717Z",
"updatedAt": "2019-03-29T04:46:27.717Z",
"__v": 0
},
{
"_id": "5c9da32e218b1b0c7457cd51",
"name": "Camisas",
"parent_key": "5c9da323218b1b0c7457cd4f",
"createdAt": "2019-03-29T04:46:38.524Z",
"updatedAt": "2019-03-29T04:46:38.524Z",
"__v": 0
}
];
function getNestedChildren(categories, parent_key) {
let newCategories = [];
categories.forEach(categorie => {
if(categorie.parent_key == parent_key) {
let childs = getNestedChildren(categories, categorie._id)
if(childs.length) {
categorie.childs = childs;
}
newCategories.push(categorie)
}
});
return newCategories
}
console.log(getNestedChildren(categories));
</script>
</html>