【问题标题】:Convert nested Json to flat Json with parentId to every node将嵌套的 Json 转换为带有 parentId 的平面 Json 到每个节点
【发布时间】:2019-04-04 07:36:06
【问题描述】:

以下 Json 结构是 Neo4J apoc 查询的结果。我想将这个嵌套的 Json 转换为平面 Json 结构,如第二个 json 所示。

[
    {
      "child1": [
        {
          "_type": "EntityChild1",
          "name": "Test222",
          "_id": 2
        }
      ],
      "child2": [
        {
          "_type": "EntityChild2",
          "name": "Test333",
          "_id": 3,
          "child2_child1": [
            {
              "_type": "EntityChild2_1",
              "name": "Test444",
              "_id": 6,
              "child2_child1_child1": [
                {
                  "_type": "EntityChild2_1_1",
                  "name": "Test555",
                  "_id": 7
                }
              ]
            }
          ]
        }
      ],
      "_type": "EntityParent",
      "name": "Test000",
      "_id": 1,
      "child3": [
        {
          "_type": "EntityChild3",
          "name": "Test111",
          "_id": 4
        }
      ],
      "child4": [
        {
          "_type": "EntityChild4",
          "name": "Test666",
          "_id": 5
        }
      ]
    }
    ]

这是我正在寻找的结果,我还希望将 parentId 附加到每个节点。如果特定节点没有父节点,则它的 parentid 应该为 -1。

[
  {
    "_type": "EntityParent",
    "name": "Test000",
    "_id": 1,
    "parentid": -1
  },
  {
    "_type": "EntityChild1",
    "name": "Test222",
    "_id": 2,
    "parentid": 1
  },
  {
    "_type": "EntityChild2",
    "name": "Test333",
    "_id": 3,
    "parentid": 1
  },
  {
    "_type": "EntityChild2_1",
    "name": "Test444",
    "_id": 6,
    "parentid": 3
  },
  {
    "_type": "EntityChild2_1_1",
    "name": "Test555",
    "_id": 7,
    "parentid": 6
  },
  {
    "_type": "EntityChild3",
    "name": "Test111 ",
    "_id": 4,
    "parentid": 1
  },
  {
    "_type": "EntityChild4",
    "name": "Test666",
    "_id": 5,
    "parentid": 1
  }
]

如果需要任何进一步的信息,请告诉我。

【问题讨论】:

  • 请添加您的代码以及出了什么问题。
  • 实际上我无法弄清楚如何通过向每个节点添加 parentid 将嵌套 json 转换为平面 json。

标签: javascript json typescript neo4j


【解决方案1】:

您可以通过使用一个函数来采用迭代和递归的方法,该函数接受一个数组和一个实际关卡的父 ID。

如果属性以child 开头,它会使用实际的_id 再次调用该函数,并将所有项目推送到结果集中。

function getFlat(array, parentid) {
    return array.reduce((r, o) => {
        var temp = {};
        r.push(temp);
        Object.entries(o).forEach(([k, v]) => {
            if (k.startsWith('child')) {
                r.push(...getFlat(v, o._id));
            } else {
                temp[k] = v;
            }
        });
        temp.parentid = parentid;
        return r;
    }, []);
}


var data = [{ child1: [{ _type: "EntityChild1", name: "Test222", _id: 2 }], child2: [{ _type: "EntityChild2", name: "Test333", _id: 3, child2_child1: [{ _type: "EntityChild2_1", name: "Test444", _id: 6, child2_child1_child1: [{ _type: "EntityChild2_1_1", name: "Test555", _id: 7 }] }] }], _type: "EntityParent", name: "Test000", _id: 1, child3: [{ _type: "EntityChild3", name: "Test111", _id: 4 }], child4: [{ _type: "EntityChild4", name: "Test666", _id: 5 }] }],
    flat = getFlat(data, -1);

console.log(flat);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 非常感谢您的快速响应,它正在按预期工作。你能告诉我如何使用打字稿来达到同样的效果吗?
  • 抱歉打字稿,如果不知道的话。
  • 非常感谢 Nina Scholz
猜你喜欢
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-29
  • 1970-01-01
  • 1970-01-01
  • 2019-07-03
  • 1970-01-01
相关资源
最近更新 更多