title: Find object from an array of nested objects by key in JavaScript
body: <p>I have a JSON nested object tree like this.
  How can I get the specified object by passing an ID value to a function. (ID is a key in all nested objects)</p>
  <p>I tried with recursion &amp; JSON.stringify().findObject() but somehow they are not working.</p>
  <pre><code>{
      &quot;id&quot;: &quot;A&quot;,
      &quot;name&quot;: &quot;Item A&quot;,
      &quot;child&quot;: [
          {
              &quot;id&quot;: &quot;B&quot;,
              &quot;name&quot;: &quot;Item B&quot;,
              &quot;child&quot;: [
                  {
                      &quot;id&quot;: &quot;C&quot;,
                      &quot;name&quot;: &quot;Item C&quot;
                      &quot;child&quot;: []
                  }
              ]
          },
          {
              &quot;id&quot;: &quot;D&quot;,
              &quot;name&quot;: &quot;Item D&quot;,
              &quot;child&quot;: []
          }
      ]
  }
  
  
  //Calling like this
  var result = findObject(&quot;C&quot;);
  </code></pre>
  <p>Then <code>console.log(result);</code> Should be</p>
  <pre><code>{
    &quot;id&quot;: &quot;C&quot;,
    &quot;name&quot;: &quot;Item C&quot;
    &quot;child&quot;: []
  }
  </code></pre>