-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
52 lines (50 loc) · 961 Bytes
/
Copy pathindex.html
File metadata and controls
52 lines (50 loc) · 961 Bytes
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<!-- <link href="https://fonts.font.im/css?family=Do+Hyeon" rel="stylesheet"> -->
<style></style>
</head>
<script>
window.onload = () => {
// object
let obj = {
a: {
b: {
c: {
d: 1,
},
},
},
e: 2,
f: {
g: 3,
},
};
// 输出
// result2 = [[a, e, f], [b, g], [c], [d]];
function getBfs(data) {
let depth = -1;
let result = [];
function bfs(obj) {
depth++;
if (typeof obj === "object" && Object.keys(obj).length > 0) {
Object.keys(obj).forEach((key) => {
result[depth] ? result[depth].push(key) : (result[depth] = [key]);
bfs(obj[key]);
});
}
depth--;
}
bfs(data);
return result;
}
console.log(getBfs(obj));
}
</script>
<body>
我是初始值
</body>
</html>