title: Extract nested <a> elements from a JSON string
body: <p>I am trying to find all "a" keys in a JSON string and save them in an array. Since they may be nested at different levels, I'm using recursion to traverse each key's children to exhaustively list all "a" keys. However, I cannot wrap my head around recursing the child nodes and appending any found "a" keys to the returned array. Any help here would be appreciated.</p>
  
  <p>The function for traversing the JSON string:</p>
  
  <pre><code>searchForKeys = (keyValue, obj) =&gt; {
  
    let arrayOfKeys = [];
  
    if (obj instanceof Array) {
      obj.forEach((item, index) =&gt; {
  
       if(typeof(item) === 'object' &amp;&amp; Object.keys(item)[index] === keyValue) {
          console.log('pushing found item to return array');
          arrayOfKeys.push(...item);
          console.log(arrayOfKeys);
       } 
      });
    }
    else if (typeof(obj) === 'object') {
      if (Object.keys(obj).length &gt; 0) {
        let itemName;
        for(let i = 0; i &lt; Object.keys(obj).length; i++) {
          itemName = Object.keys(obj)[i];
          if (itemName === keyValue) {
            console.log('pushing found item to return array');
            arrayOfKeys.push(obj[itemName]);
          }
        }
        console.log('arrayOfKeys.length:', arrayOfKeys.length);
        arrayOfKeys.push(searchForKeys(keyValue, obj[itemName]));
    }
    return arrayOfKeys;
    }
  };
  
  let returnedArray = searchForKeys("a", dummyJSON);
  
  console.log("array content:\n", [...returnedArray]); 
  console.log("array length", returnedArray.length);
  </code></pre>
  
  <p>The expected result is returnedArray to contain all four "a" keys from the JSON object.</p>
  
  <p>The source data:</p>
  
  <pre><code>const dummyJSON = {
    "a": {
      "@attributes": {
        "href": "/URL/2021.html?q=link+value",
        "title": "Lautsprecher",
        "class": "catfilter-list-link",
        "data-gtm-event": "filter.click",
        "data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
        "data-metric-click": "mainsearch-catfilter-click"
      },
      "#text": "Lautsprecher"
    },
    "ul": {
      "@attributes": {
        "class": "catfilter-list"
      },
      "#text": [],
      "li": [
        {
          "@attributes": {
            "class": "catfilter-list-item"
          },
          "#text": [
            "",
            ""
          ],
          "a": {
            "@attributes": {
              "href": "/URL/8412.html?q=link+value",
              "title": "Handy-Ladegeräte",
              "class": "catfilter-list-link",
              "data-gtm-event": "filter.click",
              "data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
              "data-metric-click": "mainsearch-catfilter-click"
            },
            "#text": "Handy-Ladegeräte"
          }
        },
        {
          "@attributes": {
            "class": "catfilter-list-item"
          },
          "#text": [
            "",
            ""
          ],
          "a": {
            "@attributes": {
              "href": "/URL/26136.html?q=link+value",
              "title": "Kabellose Ladegeräte",
              "class": "catfilter-list-link",
              "data-gtm-event": "filter.click",
              "data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
              "data-metric-click": "mainsearch-catfilter-click"
            },
            "#text": "Kabellose Ladegeräte"
          }
        },
        {
          "@attributes": {
            "class": "catfilter-list-item"
          },
          "a": {
            "@attributes": {
              "href": "/URL/2021.html?q=link+value",
              "title": "Lautsprecher",
              "class": "catfilter-list-link",
              "data-gtm-event": "filter.click",
              "data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
              "data-metric-click": "mainsearch-catfilter-click"
            },
            "#text": "Lautsprecher"
          },
          "#text": [
            "",
            ""
          ],
          "a": {
            "@attributes": {
              "href": "/URL/2021.html?q=link+value",
              "title": "Lautsprecher",
              "class": "catfilter-list-link",
              "data-gtm-event": "filter.click",
              "data-gtm-payload": "{\"event_category\":\"mainsearch\",\"event_action\":\"quickfilter.click\",\"event_label\":\"quickfilter\",\"position\":\"1\",\"location\":\"headline\"}",
              "data-metric-click": "mainsearch-catfilter-click"
            },
            "#text": "Lautsprecher"
          }
        }
      ]
    }
  };
  </code></pre>