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.
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)));
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.
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)));