jpdan 2009-09-03 16:37
浏览 295
已采纳

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循环了。
请帮忙具体能运行了再解答!谢谢。

  • 写回答

6条回答 默认 最新

  • iteye_10013 2009-09-04 08:54
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题