-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursive.js
57 lines (52 loc) · 1.44 KB
/
recursive.js
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
// Nested JSON object
var myObj = {
name: 'level 1',
layer: {
name: 'level 2',
layer: {
name: 'level 3',
layer: {
name: 'level 4',
layer: {
name: 'level 5'
}
}
}
}
};
/*
* This function recursively grabs the name of the
* object and pushes it to the array. It starts at
* the top-most level and recurses down each layer
*/
(function() {
input1 = [];
(function parse1(object) {
var name = object.name;
input1.push(name);
document.write('</br>' + input1);
if(object.layer) {
parse1(object.layer);
}
return input1;
}(myObj)) // Pass our nested object to the parse1 function and immediately invoke it
}()) // Immediately invoke the `closure` function
/*
* This function first recurses down to the deepest
* layer and pushes that name on to the array. As
* it recurses back up, it pushes the next name on
* to the array.
*/
(function() {
input2 = [];
(function parse2(object) {
if(object.layer) {
parse2(object.layer);
}
var name = object.name;
input2.push(name);
console.log(name);
document.write('</br>' + input2);
return input2;
}(myObj)) // Pass our nested object to the parse1 function and immediately invoke it
}()); // Immediately invoke the `closure` function