title: Find an element in an array recursively
body: <p>I have an array of objects. Every object in the array has an id and an item property that is an array containing other object. I need to be able to find an element in an array by id. Here is a sample of what I have done so far, but the recursive function is always returning undefined.</p>
  
  <p>How can I quit the function and return the item when I have called the function recursively several times?</p>
  
  <pre><code>   $(function () {
      var treeDataSource = [{
          id: 1,
          Name: "Test1",
          items: [{
              id: 2,
              Name: "Test2",
              items: [{
                  id: 3,
                  Name: "Test3"
              }]
          }]
      }];
      var getSubMenuItem = function (subMenuItems, id) {
          if (subMenuItems &amp;&amp; subMenuItems.length &gt; 0) {
              for (var i = 0; i &lt; subMenuItems.length; i++) {
                  var item;
                  if (subMenuItems[i].Id == id) {
                      item = subMenuItems[i];
                      return item;
                  };
                  getSubMenuItem(subMenuItems[i].items, id);
              };
          };
      };
      var searchedItem = getSubMenuItem(treeDataSource, 3);
      alert(searchedItem.id);
  });
  </code></pre>
  
  <p><a href="http://jsfiddle.net/6VZxB/9/" rel="noreferrer"><strong>jsFiddle</strong></a></p>