简体   繁体   English

如何递归遍历指向其他对象数组的嵌套对象数组?

[英]How to recursively loop through nested array of objects that point to other arrays of object?

I have the following JSON response from an API.我有来自 API 的以下 JSON 响应。 My goal is loop through each term key inside the example_terms array and grab all $id values and return a final array of all values, including the ids inside each term_parents array.我的目标是遍历example_terms数组中的每个term键并获取所有$id值并返回所有值的最终数组,包括每个term_parents数组中的 id。 So my final result would be something like: ['taxonomy/24232', 'taxonomy/11179', 'taxonomy/12058', 'taxonomy/11053', etc..] .所以我的最终结果将类似于: ['taxonomy/24232', 'taxonomy/11179', 'taxonomy/12058', 'taxonomy/11053', etc..] The depth of each term object is variable, so I know I have to somehow do this recursively.每个term对象的深度是可变的,所以我知道我必须以某种方式递归地执行此操作。 I'm not sure how to do it properly, so any help is appreciated.我不知道如何正确地做到这一点,所以任何帮助表示赞赏。

   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"
                }]
              }]
            }]
          }],
        }
      }
    ]

You could have a look to objects and get the wanted property and the nested value or return an empty array.您可以查看对象并获取所需的属性和嵌套值或返回一个空数组。

This approach features an array as result.这种方法的特点是一个数组。 This is spreadable and allows a recursive function without having the intermediate result to store.这是可扩展的,并且允许递归函数而无需存储中间结果。

Let start with the exit condition.让我们从退出条件开始。 This is a check for not being an truthy value and no object.这是对不是真实值和没有对象的检查。 Then return an empty array (this check is reversed below).然后返回一个空数组(此检查在下面颠倒)。

For a truthy value which is an object, too, take a check for the wanted key and if found get the value, otherwise take an empty array for spreading.对于作为对象的真值,也检查所需的键,如果找到则获取值,否则获取空数组进行传播。

Then get the values from the object and take a flat map with it.然后从对象中获取值并使用它制作平面地图。 The result of this call is spreaded to the result array for return.此调用的结果将传播到结果数组以供返回。

 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; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM