我有一组对象,具有未定义的深度。现在我希望名称输出如下:
Top Level
Top Level > Sub 1
Top Level > Sub 1 > Sub 1-2
Top Level > Sub 2
但是,我总是只得到最低的元素。我不知道我还可以更改什么来正确格式化名称。
parseCategories(categories, stackCounter = 0) {
categories.forEach(c => {
this.res[stackCounter] = { name: c.attributes.name, id: c.id };
if(c.children.length >= 1) {
this.parseCategories(c.children, stackCounter + 1);
}
});
return this.res;
}
我的对象看起来像这样:
{
"data":[
{
"type":"categories",
"id":"1",
"attributes":{
"name":"Top Level"
},
"children":[
{
"type":"categories",
"id":"2",
"attributes":{
"name":"Sub 1"
},
"children":[
{
"type":"categories",
"id":"4",
"attributes":{
"name":"Sub 1-2"
},
"children":[
]
}
]
},
{
"type":"categories",
"id":"3",
"attributes":{
"name":"Sub 2"
},
"children":[
]
}
]
}
]
}
弑天下
繁星coding
相关分类