title: How to find a object in a nested array using recursion in JS
body: <p>Consider the following deeply nested array:</p>
  
  <pre><code>const array = [
      {
          id: 1,
          name: "bla",
          children: [
              {
                  id: 23,
                  name: "bla",
                  children: [{ id: 88, name: "bla" }, { id: 99, name: "bla" }]
              },
              { id: 43, name: "bla" },
              {
                  id: 45,
                  name: "bla",
                  children: [{ id: 43, name: "bla" }, { id: 46, name: "bla" }]
              }
          ]
      },
      {
          id: 12,
          name: "bla",
          children: [
              {
                  id: 232,
                  name: "bla",
                  children: [{ id: 848, name: "bla" }, { id: 959, name: "bla" }]
              },
              { id: 433, name: "bla" },
              {
                  id: 445,
                  name: "bla",
                  children: [
                      { id: 443, name: "bla" },
                      {
                          id: 456,
                          name: "bla",
                          children: [
                              {
                                  id: 97,
                                  name: "bla"
                              },
                              {
                                  id: 56,
                                  name: "bla"
                              }
                          ]
                      }
                  ]
              }
          ]
      },
      {
          id: 15,
          name: "bla",
          children: [
              {
                  id: 263,
                  name: "bla",
                  children: [{ id: 868, name: "bla" }, { id: 979, name: "bla" }]
              },
              { id: 483, name: "bla" },
              {
                  id: 445,
                  name: "bla",
                  children: [{ id: 423, name: "bla" }, { id: 436, name: "bla" }]
              }
          ]
      }
  ];
  </code></pre>
  
  <p>How would I grab a certain object by key that might be deeply nested, using recursion?
  I have tried this, but this won't work for nesting deeper than 2 levels, it then just returns <code>undefined</code>:</p>
  
  <pre><code>const findItemNested = (arr, itemId, nestingKey) =&gt; {
      for (const i of arr) {
          console.log(i.id);
          if (i.id === itemId) {
              return i;
          }
          if (i[nestingKey]) {
              findItemNested(i[nestingKey], itemId, nestingKey);
          }
      }
  };
  </code></pre>
  
  <p>The result should be:</p>
  
  <p><code>const res = findItemNested(array, 959, "children"); &gt;&gt; { id: 959, name: "bla" }</code></p>
  
  <p>This can perhaps also be achieved using <code>.find</code>, or just to flatten the array (by the children key), but using recursion seems like the most logical solution to me. Does anybody have a solution to this?</p>
  
  <p>Thanks in advance :).</p>