title: AngularJS : get back data from a json array with an id
body: <p>I have a json file where i am stocking informations from all the people in my database. I actually use it to display first name, last name in a web page and i want to add the possibility to display the details of every person.</p>
  
  <p>To do so i'm using the id of the person like this :</p>
  
  <pre><code>.when('/people/:id', {templateUrl: 'partials/people-detail.html'})
  </code></pre>
  
  <p>It works pretty well, i have a page generated for every person, which is nice. But now I would like to get the information of the person back.</p>
  
  <p>The easiest way would have been to have a json file for every person, but i don't particularly like the idea of having so much file. 
  So my actual idea is to iterate through the people.json file to find the good one and using it but it's not working.</p>
  
  <p>Here's my controller :</p>
  
  <pre><code>var PeopleController = angular.module ('PeopleController', []);
  
  PeopleController.controller('PeopleDetailCtrl', ['$scope', '$routeParams', '$http',
      function($scope, $routeParams, $http) {
          $scope.search = function() {
              var url = 'data/people.json';
              $http.get(url).success(httpSuccess).error(function() {
                  alert('Unable to get back informations :( ');
              });
          }
          httpSuccess = function(response) {
              $scope.persons = response;
          }
  
          function getById(arr, id) {
              for (var d = 0, len = arr.length; d &lt; len; d += 1) {
                  if (arr[d].id === id) {
                      return arr[d];  
                  }
              }
          }
          $scope.search();
          $scope.person = getById($scope.persons,$routeParams.id);
      }]);
  </code></pre>
  
  <p>Well, maybe my solution is bad since it doesn't work, but i didn't find another way to do so.</p>
  
  <p>Now i'm all yours :)</p>
  
  <p>Thanks for reading.</p>