【问题标题】:How to recursively loop through nested array of objects that point to other arrays of object?如何递归循环遍历指向其他对象数组的嵌套对象数组?
【发布时间】:2020-03-08 06:06:19
【问题描述】:

我有来自 API 的以下 JSON 响应。我的目标是循环遍历 example_terms 数组中的 each term 键并获取所有 $id 值并返回所有值的最终数组,包括 each中的 id > term_parents 数组。所以我的最终结果将类似于:['taxonomy/24232', 'taxonomy/11179', 'taxonomy/12058', 'taxonomy/11053', etc..]每个term 对象的深度是可变的,所以我知道我必须以某种方式递归地执行此操作。我不确定如何正确执行此操作,因此感谢您的帮助。

   example_terms: [
       {
        rejected: false,
        term: {
          $id: "taxonomy/24232",
          term_parents: [{
            $id: "taxonomy/15197",
            term_parents: [{
              $id: "taxonomy/11179",
              term_parents: [{
                $id: "taxonomy/11013"
              }]
            }]
          }],
        }
      },
      {
        rejected: false
        term: {
          $id: "taxonomy/12058",
          term_parents: [{
            $id: "taxonomy/12110",
            term_parents: [{
              $id: "taxonomy/12178",
              term_parents: [{
                $id: "taxonomy/11013"
              }]
            }]
          }],
        }
      },
      {
        rejected: false
        term: {
          $id: "taxonomy/10769",
          term_parents: [{
            $id: "taxonomy/11401",
            term_parents: [{
              $id: "taxonomy/11807",
              term_parents: [{
                $id: "taxonomy/11374",
                term_parents: [{
                  $id: "taxonomy/11053"
                }]
              }]
            }]
          }],
        }
      }
    ]

【问题讨论】:

    标签: javascript arrays recursion


    【解决方案1】:

    您可以查看对象并获取所需的属性和嵌套值,或者返回一个空数组。

    这种方法以数组作为结果。这是可扩展的,并且允许递归函数而无需存储中间结果。

    让我们从退出条件开始。这是对不是真实值和没有对象的检查。然后返回一个空数组(这个检查在下面反转)。

    对于一个真实的值也是一个对象,检查想要的键,如果找到则获取值,否则取一个空数组进行传播。

    然后从对象中获取值并使用它获取平面地图。本次调用的结果传播到结果数组中返回。

    function getValues(object) {
        return object && typeof object === 'object'
            ? [
                  ...('$id' in object ? [object.$id] : []), 
                  ...Object.values(object).flatMap(getValues)
            ]
            : [];
    }
    
    var data = { example_terms: [{ rejected: false, term: { $id: "taxonomy/24232", term_parents: [{ $id: "taxonomy/15197", term_parents: [{ $id: "taxonomy/11179", term_parents: [{ $id: "taxonomy/11013" }] }] }] } }, { rejected: false, term: { $id: "taxonomy/12058", term_parents: [{ $id: "taxonomy/12110", term_parents: [{ $id: "taxonomy/12178", term_parents: [{ $id: "taxonomy/11013" }] }] }] } }, { rejected: false, term: { $id: "taxonomy/10769", term_parents: [{ $id: "taxonomy/11401", term_parents: [{ $id: "taxonomy/11807", term_parents: [{ $id: "taxonomy/11374", term_parents: [{ $id: "taxonomy/11053" }] }] }] }] } }] },
        result = getValues(data);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

    • 虽然我不完全理解你做了什么,但该解决方案有效。你能深入解释一下你的代码吗?特别是,我不明白您如何将扩展运算符与 Object.values 一起使用,以及您如何准确地使用 flatMap 函数。
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多