-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
46 lines (41 loc) · 971 Bytes
/
test.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
<script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
function remove(arrPerson,objPropery,objValue)
{
return $.grep(arrPerson, function(cur,i){
return cur[objPropery]!=objValue;
});
}
function get(arrPerson,objPropery,objValue)
{
return $.grep(arrPerson, function(cur,i){
return cur[objPropery]==objValue;
});
}
function build(arrPerson,result)
{
$.each(result,function(i,v){
var t = get(arrPerson,"parent_id",v["id"]);
if(t){
result[i]["child"] = t;
return build(arrPerson,result[i]["child"]);
}else{
return result;
}
});
return result;
}
var nav = [
{"id":"1","parent_id":"0"},
{"id":"2","parent_id":"1"},
{"id":"3","parent_id":"1"},
{"id":"4","parent_id":"2"},
{"id":"5","parent_id":"4"},
{"id":"6","parent_id":"4"},
{"id":"7","parent_id":"0"},
{"id":"8","parent_id":"7"},
{"id":"9","parent_id":"8"},
];
var test = get(nav,"parent_id","0");
console.log(build(nav,test));
</script>