title: Extracting a corresponding value in json object in AngularJS
body: <p>I am using AngularJS. I have a json object as below;</p>
  
  <pre><code>info = [
  {
      "name": "Tom",
      "id": "111"
  },
  {
      "name": "Sam",
      "id": "222"
  },
  {
      "name": "James",
      "id": "333"
  }
  ]
  </code></pre>
  
  <p>I want to have a function such that when a matching name is found, some action is taken (in this -case, return the corresponding id.) In other words, if the input matching name is 'Tom', I want to return the id '111' based on the json object above.</p>
  
  <p>I wrote some code to find a matching name.</p>
  
  <pre><code>$scope.getIdFromName = function()
          {
              angular.forEach(info, function(value, key)
              {
                  //$scope.searchByName contains the name to be matched
                  if (key === 'name' &amp;&amp; value === $scope.searchByName)
                  {
                      //$scope.searchById is the id to be returned
                      $scope.searchById = key;
                      alert("found");
                  }
              }); 
          };
  </code></pre>
  
  <p>Where did the code go wrong? Or is it so wrong that it is better to be completely rewritten? Any suggestions (does not need to be angularjs) will be most welcome. Thank you very much.</p>