1

Input:

{ key : "value" ,
  list : [
    { 
        key : "values1" ,
        list : [
            { key : "value2" , list :[{ key : "simpleValue" } ]
            }
            ]
    },
    {
        key : "value3"
    }
]
}

Output:

{key : ["value" , "values1" , "values2" , "simpleeValue", "values3"]}

the code that I wrote for conversion is

var outputArray=new Array();
var count=0;
  function recursion(testData){



if(testData.key==undefined)
{

return ;

}


else
{
    outputArray[count]=testData.key;
    count++;

for(var k in testData.list)
    {

    testData1=testData.list[k];
    recursion(testData1);
    recursion(testData.key);
    }


}

return outputArray;
}

The output will only give me the value list an array,like [ 'value', 'values1', 'value2', 'simpleValue', 'value3' ], how do I use the hash method to get the correct output?

4

2 回答 2

0

嗯,这样的??

var inpObj = { key : "value" ,list : [
                    {
                        key : "values1"
                    },
                    {
                        key : "value3"
                    }
                ]
            };

var outputObj = new Object;
var outputArray = new Array();

function recursion(testData){
    if(testData.key==undefined)
    {
        return;
    }
    else
    {
        var newKey={};
        //alert(testData.key);
        outputArray.push(testData.key);

        for(var k in testData.list)
        {
            testData1=testData.list[k];
            recursion(testData1);
            recursion(testData.key);
        }
    }

    return outputArray;
}

recursion(inpObj);
if (outputObj.key == undefined) outputObj.key = outputArray;
alert(outputObj.key.join(", "));
于 2012-05-03T08:39:01.173 回答
0

我把这件事搞定了

var outputArray=new Array();

function recursion(testData){


if(testData.key==undefined)
{

return ;

}


else
{

    //alert(testData.key);
    outputArray.push(testData.key);

for(var k in testData.list)
    {
    testData1=testData.list[k];
    recursion(testData1);
    recursion(testData.key);
    }


}


var l={};
l.key=outputArray;
return l;
}
于 2012-05-05T13:48:58.007 回答