javascript递归调用

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){}//name为地区名,arr为地区的数组集合
现在我需要地区为name的所有信息,包括其子地区信息

比如name='杭州市',则返回{name:'杭州市',children:[{name:'西湖区',children:null},{name:'萧山区',children:''}]},
name='浙江省',则返回name:'浙江省',children:
[
{name:'杭州市',children:[{name:'西湖区',children:null},{name:'萧山区',children:''}]},
{name:'温州市',children:[{name:'乐清市',children:null},{name:'苍南县',children:''}]}
]
}

//这个方法不行//
function getReionByRegionName(name,array){
var o;
var nextObj;
for (i = 0; i < array.length; i++) {
o = array[i];
if(name == o.name){
return array[i];
}else{
if(null!=ch){
return document.getRegionByName(name,ch);
}else{
if(''==ch){
.........................
}
continue;
}
}
}

当然也要注意性能,数据有点多!
各位javascript达人帮哈忙!thanks

[b]问题补充:[/b]
atian25
这个肯定不行啊。
因为当递归当县一级的时候children都为空了。你这样肯定报错。
[b]问题补充:[/b]
还是不行啊。
当递归到县级的时候children都为空了,你再迭代就出去for循环了。
请帮忙具体能运行了再解答!谢谢。

[quote]问题补充:
还是不行啊。
当递归到县级的时候children都为空了,你再迭代就出去for循环了。
请帮忙具体能运行了再解答!谢谢。 [/quote]

[size=medium]你确定你跑过我的代码?[/size]

[code="javascript"]
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)));
[/code]

//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":""}

仅实现功能:
[code="javascript"]
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) {
for (var i = 0; i < array.length; i++) {
if (name == array[i].name) {
return array[i];
} else {
return getReionByRegionName(name, array[i].children);
}
}
}
console.log(getReionByRegionName('杭州市', array));
console.log(getReionByRegionName('浙江省', array));
/* paste in your code and press Beautify button */
if ('this_is' == /an_example/) {
do_something();
} else {
var a = b ? (c % d) : e[f];
}[/code]

后面的那段贴多了,删掉即可

[code="java"] 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){
for(var i=0;i<array.length;i++){
if(name==array[i].name){
return array[i];
}else{
var temp = null;
if(array[i].children!=null && array[i].children!=''){
temp = getReionByRegionName(name,array[i].children);
}
if(temp!=null){
return temp;
}else{
continue;
}
}
}
return null;
}
console.log(getReionByRegionName('浙江省',array));
console.log(getReionByRegionName('杭州市',array));
console.log(getReionByRegionName('温州市',array));
console.log(getReionByRegionName('苍南县',array));[/code]

再整理下:
[code="javascript"]
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(getReionByRegionName('浙江省',array));
console.log(getReionByRegionName('杭州市',array));
console.log(getReionByRegionName('温州市',array));
console.log(getReionByRegionName('苍南县',array));
console.log(getReionByRegionName('乐清市',array));
console.log(getReionByRegionName('西湖区',array));
[/code]

上面的代码可以跑啊,没有问题。你说的“跑到县级子节点为空”是什么意思?因为你给的数据下面,县级本来就没有子节点啊,比如“苍南”,不是本来就没子节点么?