我有2种类型的对象,group
和item
。一个组可以有children
,它可以是array of groups
或array of items
。从javascript中的嵌套对象中获取最深层次的子对象
我已经结束了一系列嵌套组(它可以是无限级深层),并且我需要检索所有项目,无论深度有多少级别,只有一个组可以使用。
有没有办法在以下数据结构中检索顶级组中的所有项目?
{
type: 'group',
children: [
{
type: 'group',
children: [
{
type: 'group',
children: [{type:'item'}, {type:'item'}, {type:'item'}]
},
{
type: 'group',
children: [{type:'item'}, {type:'item'}, {type:'item'}]
},
{
type: 'group',
children: [{type:'item'}, {type:'item'}, {type:'item'}]
},
]
},
{
type: 'group',
children: [
{
type: 'group',
children: [{type:'item'}]
},
{
type: 'group',
children: [{type:'item'}]
},
{
type: 'group',
children: [{type:'item'}]
},
]
},
{
type: 'group',
children: [
{
type: 'group',
children: [{type:'item'}, {type:'item'}]
},
{
type: 'group',
children: [{type:'item'}, {type:'item'}]
},
{
type: 'group',
children: [{type:'item'}, {type:'item'}]
},
]
},
]
}
你的数据结构是无效的。数组在文字中没有属性。 –
如果你使它正确,然后使用递归技术来迎合这一点。 – Jai
由于在实际数组中不会有属性标签可用,因此如何区分组对象数组和项目对象数组?如果您可以显示实际数组的示例并包含组和项目对象定义,则可能更容易。 – Nope