我尝试迭代一个嵌套对象来检索由字符串标识的特定对象。在下面的示例对象中,标识符字符串是“ label”属性。我无法理解如何循环遍历树来返回适当的对象。如有任何帮助或建议,我们将不胜感激。
var cars = {
label: 'Autos',
subs: [
{
label: 'SUVs',
subs: []
},
{
label: 'Trucks',
subs: [
{
label: '2 Wheel Drive',
subs: []
},
{
label: '4 Wheel Drive',
subs: [
{
label: 'Ford',
subs: []
},
{
label: 'Chevrolet',
subs: []
}
]
}
]
},
{
label: 'Sedan',
subs: []
}
]
}