【问题标题】:Need help in flattening undeterminate deeply nested JSON Object in JavaScript需要帮助在 JavaScript 中展平不确定的深度嵌套 JSON 对象
【发布时间】:2022-06-15 08:55:26
【问题描述】:

我已经尝试了几个小时来解决这个问题,我认为这花了我太长时间,我放弃了。所以我决定在这里寻求一些帮助。

我有这个数据结构:

{
  "data": {
    "children": [
      {
        "data": {
          "body": "3 and 4 are pretty much the answer",
          "replies": {
            "data": {
              "children": [
                {
                  "data": {
                    "body": "can be expressed in a nicer way",
                    "replies": {
                      "data": {
                        "children": [
                          {
                            "data": {
                              "body": "yes",
                              "replies": ""
                            }
                          }
                        ]
                      }
                    }
                  }
                },
                {
                  "data": {
                    "body": "can be expressed in second way",
                    "replies": ""
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

我想将对象展平成这样:

{
    "body": "3 and 4 are pretty much the answer",
    "replies": [
        {
            "body": "can be expressed in a nicer way",
            "replies": [
                "body": "yes",
                "replies": ""
            ]
        },
        {
            "body": "can be expressed in second way",
            "replies": ""
        }
    ]
}

任何帮助将不胜感激,在此先感谢您!

【问题讨论】:

  • 你试过什么代码?

标签: javascript arrays json recursion flatten


猜你喜欢
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
  • 2021-12-03
  • 2022-01-13
  • 2019-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多