我有一个 JavaScript 对象:
var testJSON = {
"Divisions": {
"modes": [{
"number": 1,
"name": "Fruits",
"Fruits": [{
"name": "Mango",
"Mango": [{
"number": 1,
"name": "Nagpur"
}]
}]
}, {
"number": 4,
"name": "Coffee",
"Coffee": [{
"name": "Germany",
"Germany": [{
"number": 1,
"name": "Germany"
}]
}, {
"name": "Italy",
"Italy": [{
"number": 1,
"name": "Italy"
}]
},
]
}, {
"number": 5,
"name": "Burger",
"Burger": [{
"number": 2,
"name": "McDonalds"
}, {
"number": 1,
"name": "Ham"
}]
}]
}
};
我想将每个级别中“名称”的值获取到不同的数组..
我试过代码
function recursion(input)
{
var arrayLength=input.Divisions.modes.length;
for(var i=0;i<arrayLength;i++)
{
var elem = input[i];
if(input[i].name==undefined);
{
return;
}
recursion(elem);
}
}
var c=recursion(testJSON);
这行不通。我想这是我做错的递归部分。对象的级别不是固定的。