Javascript 遍历对象树并基于对象特性条件渲染菜单树
您好,我正在尝试从一个给定的对象创建一个html菜单,如下所示,我想知道是否有一个库可以帮助我遍历对象,而不是重新发明轮子Javascript 遍历对象树并基于对象特性条件渲染菜单树,javascript,recursion,Javascript,Recursion,您好,我正在尝试从一个给定的对象创建一个html菜单,如下所示,我想知道是否有一个库可以帮助我遍历对象,而不是重新发明轮子 var root = { category1: { depth: 0, categories: { category1: { depth: 1 }, category2: { depth: 1,
var root = {
category1: {
depth: 0,
categories: {
category1: {
depth: 1
},
category2: {
depth: 1,
products: [1, 2, 3, 4, 5]
},
category3: {
depth: 1,
categories: {
category1: {
depth: 2,
products: [1, 2, 3, 4]
},
category2: {
depth: 2,
products: [1, 2, 3, 4, 5, 6, 7]
},
category3: {
depth: 2
}
}
}
}
},
category2: {
depth: 0,
category1: {
depth: 1,
},
category2: {
depth: 1,
products:[1,2,3,4,5]
}
},
category3: {
depth: 0,
category1: {
depth: 1,
},
category2: {
depth: 1,
categories: {
category1: {
depth: 2,
products: [1, 2, 3, 4]
},
category2: {
depth: 2,
products: [1, 2, 3, 4, 5, 6, 7]
},
category3: {
depth: 2
}
}
}
},
depth: 0,
category4: {
category1: {
depth: 1,
products:[1,2,3,4,5]
},
category2: {
depth: 1,
categories: {
category1: {
depth: 2,
products: [1, 2, 3, 4]
},
category2: {
depth: 2,
products: [1, 2]
},
category3: {
depth: 2
}
}
},
category3: {
depth: 1,
}
}
};
我感兴趣的是创建菜单路径——它们的叶类别包含所有其他必须丢弃且不呈现的产品的节点。产品也可以在1级和2级类别中找到。每个类别都有一个深度指示器属性,指示类别的级别
例如,如果根类别没有包含产品的叶级类别,则不会渲染该类别,或者也不能渲染包含产品的叶的深度1类别。什么是菜单?列表?带有嵌套子菜单的菜单是的列表什么是菜单?列表?具有嵌套子菜单的菜单是列表