-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.html
78 lines (71 loc) · 1.33 KB
/
trie.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
<script type="text/javascript">
var data = [{
"province": "浙江",
"city": "杭州",
"name": "西湖"
},{
"province": "浙江",
"city": "杭州",
"name": "东湖"
}, {
"province": "四川",
"city": "杭州",
"name": "锦里"
}, {
"province": "四川",
"city": "成都",
"name": "方所"
}, {
"province": "四川",
"city": "阿坝",
"name": "九寨沟"
}];
var transObject = function(object, keys){
var hashTable = {},
res = [];
for(var i = 0, _len = object.length; i < _len; i++){
var arr = res,
cur = hashTable;
for(var j = 0, __len = keys.length; j < __len; j++){
var key = keys[j],
field = object[i][key];
if(!cur[field]){
var pusher = {
value: field
}, tmp;
if(j !== (__len - 1)){
tmp = [];
pusher.children = tmp;
}
cur[field] = {$$pos: arr.push(pusher) - 1};
cur = cur[field];
arr = tmp;
}else{
cur = cur[field];
arr = arr[cur.$$pos].children;
}
}
}
return res;
}
var d = transObject(data, ["province", "city", "name"]);
console.log(d);
var a = function(){
console.log(1);
return false;
}
var b = 1;
if(b === 1){
a();
console.log(2);
}
</script>
</html>