var xx=[
//id,pid,value
[1,0,'中国']
,[2,1,'美国']
,[3,1,'英国']
,[4,3,'伦敦']
,[5,4,'伦敦']
]

解决方案 »

  1.   

    var xx=[
    //id,pid,value
    [1,0,'中国']
    ,[2,1,'美国']
    ,[3,1,'英国']
    ,[4,3,'伦敦']
    ,[5,4,'唐宁街']
    ,[6,5,'35号']
    ];
    var gews="";
    var childrenCount=0;
    var children="";
    function getChild(id)
    {
    for (var i = 0; i < xx.length; i++) {
    if (xx[i][1]==id) {
    childrenCount++;
    if (childrenCount>1) {
    children+=","
    }
    children+=xx[i][0];
    var cc=getChild(xx[i][0]);
    if (cc=="") {
    children+=cc;
    }
    }
    }
    return children;
    }alert(getChild(1));