【发布时间】:2021-08-08 21:56:21
【问题描述】:
我有一个嵌套的 json 数组对象,如下所示
var inputData = [
{
"id": "P1",
"parentTopicId": null,
"title": "demo title 1",
"children": [
{
"id": "P1-Child1",
"parentTopicId": "P1",
"title": "demo title a",
"children": [
{
"id": "id1.1",
"parentTopicId": "P1Child1",
"title": "some demo title b",
"children": [
{
"id": "id1.1.1",
"parentTopicId": "id1.1",
"title": "demo title",
"children": []
}
]
}
]
},
{
"id": "P1-Child2",
"parentTopicId": "P1",
"title": "some title 2",
"children": [
{
"id": "id1.2",
"parentTopicId": "P1-Child2",
"title": "demo titlename",
"children": []
}
]
}
]
},
{
"id": "P2",
"parentTopicId": null,
"title": "Example Title B",
"children": [
{
"id": "P2-Child1",
"parentTopicId": "P2",
"title": "example title b2",
"children": [
{
"id": "id2.1",
"parentTopicId": "P2-Child1",
"title": "demo title",
"children": [
{
"id": "id2.1.1",
"parentTopicId": "id2.1",
"title": "demo titlename",
"children": []
}
]
}
]
}
]
}
]
我想要的输出是:
{
"rootId": "1",
"items": {
"1": {
"id": "1",
"children": [
"P1",
"P2"
],
"hasChildren": true,
"data": {
"title": "root"
}
},
"P1": {
"id": "P1",
"children": [
"P1-Child1",
"P1-Child2"
],
"hasChildren": true,
"data": {
"title": "demo title a"
}
},
"P2": {
"id": "P2",
"children": [
"P2-Child1"
],
"hasChildren": true,
"data": {
"title": "demo"
}
},
"P1-Child1": {
"id": "P1-Child1",
"hasChildren": true,
"children": [
"id1.1.1"
],
"data": {
"title": "demo title"
}
},
"P1-Child2": {
"id": "P1-Child2",
"children": [
"id1.2"
],
"hasChildren": true,
"data": {
"title": "demo titlename"
}
},
"P2-Child1": {
"id": "P2-Child1",
"children": [
"id2.1.1"
],
"hasChildren": true,
"data": {
"title": "demo titlename"
}
}
}
}
我不确定如何将输入数据准确转换为我需要的格式。关于如何展平 json 数组有什么建议吗?
我需要首先过滤 parentTopicIds 为 null 的输入数组,以便我可以将它们放在 items -> 1-> children 数组中。 {"rootId": "1","items": {"1": {"id": "1","children": ["P1","P2"]}。这很简单,因为我可以使用过滤器功能。但是在此之后,我怎样才能使 P1 和 P2 的孩子变平?
我发现了一些非常简单的扁平数组示例,但不符合我的要求
一种尝试是
var outputData ={"rootId": "1","items": {"1": {"children":[]}}};
inputData.filter((item) => {
//first push all nodes whose parentTopicId is null
if(item.parentTopicId == null) {
outputData.items["1"]["children"].push(item.id);
console.log(flatten(item.children)); // this does not provide the required result
}
});
function flatten(arr) {
return arr.reduce((acc, cur) => acc.concat(Array.isArray(cur) ? flatten(cur) : cur), []);
};
【问题讨论】:
标签: javascript arrays ecmascript-6 flatten