【发布时间】:2019-10-29 06:35:46
【问题描述】:
我想将此 json / 对象转换为下面的特定结构,以允许我使用 treeList 组件。
我试图构建一个递归函数,但我还没有找到解决方案。 感谢您的帮助
const data = {
parent1: {
child1: { bar: "1" },
child2: "2"
},
parent2: {
child1: "1"
}
}
到
const treeData = [
{
title: "parent1",
key: "parent1",
children: [
{
title: "child1",
key: "child1",
children: [{ title: "bar", key: "bar", value: "1" }]
},
{
title: "child2",
key: "child2",
value: "2"
}
],
},
{
title: "parent2",
key: "parent2",
children: [
{
title: "child1",
key: "child1",
value: "1"
}
]
}
]
【问题讨论】:
-
您的 treeData2 在单个对象中包含相同的键 "title","key","children" 两次,这是无效的
-
我犯了一个错误,现在已经修复了:) thx
标签: javascript arrays json tree converters