简体   繁体   中英

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. 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. So my final result would be something like: ['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. 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; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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