title: Recursive tree search in a nested object structure in JavaScript
body: <p>I'm trying to figure out how to search for a node in this JSON object recursively. I have tried something but cannot get it:</p>
  
  <pre><code>var tree = {
      "id": 1,
      "label": "A",
      "child": [
          {
              "id": 2,
              "label": "B",
              "child": [
                  {
                      "id": 5,
                      "label": "E",
                      "child": []
                  },
                  {
                      "id": 6,
                      "label": "F",
                      "child": []
                  },
                  {
                      "id": 7,
                      "label": "G",
                      "child": []
                  }
              ]
          },
          {
              "id": 3,
              "label": "C",
              "child": []
          },
          {
              "id": 4,
              "label": "D",
              "child": [
                  {
                      "id": 8,
                      "label": "H",
                      "child": []
                  },
                  {
                      "id": 9,
                      "label": "I",
                      "child": []
                  }
              ]
          }
      ]
  };
  </code></pre>
  
  <p>Here is my non-working solution, which is probably because the first node is just a value while children are in arrays:</p>
  
  <pre><code>function scan(id, tree) {
      if(tree.id == id) {
          return tree.label;
      }
  
      if(tree.child == 0) {
          return
      }
  
      return scan(tree.child);
  };
  </code></pre>