如何在JavaScript中创建一个通用函数以所需格式从嵌套对象中提取数据?

How to create a generic function to extract data from nested object in desired format in JavaScript?

提问人:Sumitram Kumar 提问时间:11/7/2023 最后编辑:Sumitram Kumar 更新时间:11/9/2023 访问量:28

问:

所以,这是我从某个地方获得的数据示例。您可以看到有字段(数组)。存储桶也可以嵌套在内部(再次是数组),直到任何级别。对我来说,每个级别和最后一个级别都很重要。我知道嵌套的水平(比如说,直到 3、5 或任何事先)。bucketsbucketskeydoc_count

const obj= {
    "aggregations": {
      "1": {
        "buckets": [
          {
            "2": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 3171,
              "buckets": [
                {
                  "3": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                      {
                        "key": "demo_gg",
                        "doc_count": 856
                      }
                    ]
                  },
                  "key": "/hello",
                  "doc_count": 8888
                },
                {
                  "3": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                      {
                        "key": "bye",
                        "doc_count": 414
                      }
                    ]
                  },
                  "key": "added*",
                  "doc_count": 4987
                },
                {
                  "3": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                      {
                        "key": "nothere",
                        "doc_count": 118
                      }
                    ]
                  },
                  "key": "/festive.txt",
                  "doc_count": 1888
                }
              ]
            },
            "key": "200",
            "doc_count": 4771
          },
          {
            "2": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 536,
              "buckets": [
                {
                  "3": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                      {
                        "key": "diemsion",
                        "doc_count": 1435
                      }
                    ]
                  },
                  "key": "/auto/auto.xml",
                  "doc_count": 1888
                },
                {
                  "3": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                      {
                        "key": "cartoon",
                        "doc_count": 34
                      }
                    ]
                  },
                  "key": "/.env",
                  "doc_count": 888
                },
                {
                  "3": {
                    "doc_count_error_upper_bound": 0,
                    "sum_other_doc_count": 0,
                    "buckets": [
                      {
                        "key": "mouse",
                        "doc_count": 8
                      }
                    ]
                  },
                  "key": "/form/admin/login",
                  "doc_count": 888
                }
              ]
            },
            "key": "404",
            "doc_count": 2052
          }
        ]
      }
    } 
}

预期成果:

[
  ['200', '/hello', 'demo_gg', 856],
  ['200', 'added*', 'bye', 414],
  ['200', '/festive.txt', 'nothere', 118],
  ['404', '/auto/auto.xml', 'dimension', 1435],
  ['404', '/.env', 'cartoon', 34],
  ['404', '/form/admin/login', 'mouse', 8]
]

注意:存储桶可以有任意级别的嵌套,因此每个数组也将水平增加。因此,我需要一个通用函数来用于任何级别的嵌套,直到它停止。谢谢。

JavaScript 数组对象 数据结构

评论

0赞 freedomn-m 11/7/2023
帧挑战:您的数据最初来自哪里(源 json)?更改数据库查询以预先提供所需的内容可能会更容易。
0赞 Sumitram Kumar 11/7/2023
@freedomn-m,这些是自动生成的日志

答: 暂无答案