javascript 递归调用

1. var array = [{
2. name: '浙江省',
3. leaf: 1,
4. children: [{
5. name: '杭州市',
6. leaf: 1,
7. children: [{
8. name: '西湖区',
9. leaf: 0,
10. children: null
11. },
12. {
13. name: '萧山区',
14. leaf: 0,
15. children: ''
16. }]
17. },
18. {
19. name: '温州市',
20. leaf: 1,
21. children: [{
22. name: '乐清市',
23. leaf: 0,
24. children: null
25. },
26. {
27. name: '苍南县',
28. leaf: 0,
29. children: ''
30. }]
31. }]
32. }];
33. function getReionByRegionName(name, array) {
34. var result = null;
35. for (var i = 0; i < array.length; i++) {
36. if (name == array[i].name) {
37. result = array[i];
38. break;
39. } else if (array[i].children != null && array[i].children != '') {
40. result = getReionByRegionName(name, array[i].children);
41. if (result != null) break;
42. }
43. }
44. return result;
45. }
46. //console.log是firebug, Ext.encode是exjs的,我只是为了输出验证,你改为自己的.
47. console.log(Ext.encode(array));
48. console.log(Ext.encode(getReionByRegionName('浙江省', array)));
49. console.log(Ext.encode(getReionByRegionName('杭州市', array)));
50. console.log(Ext.encode(getReionByRegionName('西湖区', array)));
51. console.log(Ext.encode(getReionByRegionName('萧山区', array)));
52. console.log(Ext.encode(getReionByRegionName('温州市', array)));
53. console.log(Ext.encode(getReionByRegionName('乐清市', array)));
54. console.log(Ext.encode(getReionByRegionName('苍南县', array)));

var array = [{
name: '浙江省',
leaf: 1,
children: [{
name: '杭州市',
leaf: 1,
children: [{
name: '西湖区',
leaf: 0,
children: null
},
{
name: '萧山区',
leaf: 0,
children: ''
}]
},
{
name: '温州市',
leaf: 1,
children: [{
name: '乐清市',
leaf: 0,
children: null
},
{
name: '苍南县',
leaf: 0,
children: ''
}]
}]
}];
function getReionByRegionName(name, array) {
var result = null;
for (var i = 0; i < array.length; i++) {
if (name == array[i].name) {
result = array[i];
break;
} else if (array[i].children != null && array[i].children != '') {
result = getReionByRegionName(name, array[i].children);
if (result != null) break;
}
}
return result;
}
//console.log是firebug, Ext.encode是exjs的,我只是为了输出验证,你改为自己的.
console.log(Ext.encode(array));
console.log(Ext.encode(getReionByRegionName('浙江省', array)));
console.log(Ext.encode(getReionByRegionName('杭州市', array)));
console.log(Ext.encode(getReionByRegionName('西湖区', array)));
console.log(Ext.encode(getReionByRegionName('萧山区', array)));
console.log(Ext.encode(getReionByRegionName('温州市', array)));
console.log(Ext.encode(getReionByRegionName('乐清市', array)));
console.log(Ext.encode(getReionByRegionName('苍南县', array)));


//firebug输出:
[{"name":" 浙江省","leaf":1,"children":[{"name":"杭州市","leaf":1,"children":[{"name":"西湖区","leaf":0,"children":null},{"name":"萧山区","leaf":0,"children":""}]}, {"name":"温州市","leaf":1,"children":[{"name":"乐清市","leaf":0,"children":null},{"name":"苍南县","leaf":0,"children":""}]}]}]

{"name":" 浙江省","leaf":1,"children":[{"name":"杭州市","leaf":1,"children":[{"name":"西湖区","leaf":0,"children":null},{"name":"萧山区","leaf":0,"children":""}]}, {"name":"温州市","leaf":1,"children":[{"name":"乐清市","leaf":0,"children":null},{"name":"苍南县","leaf":0,"children":""}]}]}

{"name":"杭州市","leaf":1,"children":[{"name":"西湖区","leaf":0,"children":null},{"name":"萧山区","leaf":0,"children":""}]}

{"name":"西湖区","leaf":0,"children":null}

{"name":"萧山区","leaf":0,"children":""}

{"name":"温州市","leaf":1,"children":[{"name":"乐清市","leaf":0,"children":null},{"name":"苍南县","leaf":0,"children":""}]}

{"name":"乐清市","leaf":0,"children":null}

{"name":"苍南县","leaf":0,"children":""}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值