-
Notifications
You must be signed in to change notification settings - Fork 3
/
tree.js
56 lines (53 loc) · 1.29 KB
/
tree.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
var TreeData = [{
name: 'top1',
id: 1,
status: 0,
children: [
{
name: 'top2',
id: 2,
status: 1,
children: [
{
name: 'top3',
id: 3,
status: 1
},
{
name: 'top4',
id: 4,
status: 0
}
]
}
]
}];
var getNode = function(node){
return { name: node.name, id: node.id, status: node.status }
};
var nodeRender = function(nodes, filter) {
filter = filter || function () { return true}
var arr = []
for (var j = 0; j < nodes.length; j++) {
var node = nodes[j];
if (filter(node)) {
arr = arr.concat(getNode(node));
}
if( node.children !== undefined ) {
var children = node.children;
for (var i = 0; i < children.length; i++) {
var child = children[i];
if (filter(children)) {
arr = arr.concat(getNode(child));
}
var arrs = nodeRender(child.children).filter(filter)
arr = arr.concat(arrs);
}
}
}
return arr;
}
console.log(nodeRender(TreeData));
console.log(nodeRender(TreeData, function (node) {return node.name === 'top4'}));
console.log(nodeRender(TreeData, function (node) {return node.status === 0}));
console.log(nodeRender(TreeData, function (node) {return node.id > 2 }));