title: How to get a single object by value in AngularJS Factory
body: <p>How do I get a single object from the flights array? I am first calling the getFlights() function in one view, and then I want to use the getFlight(flightId) in a another view when going to the page for a specific flight.</p>
  
  <p>When I call the getFlight(flightId) however, it contains no data. </p>
  
  <pre><code>angular.module('myApp.factories', []).
      factory('flightFactory', function ($http) {
  
          var flights = {};
  
          return {
              getFlights: function () {
                  return $http.get('app/data/flights.json').then(function (response) {
                      flights = response;
                      return flights;
                  });
              },
              getFlight: function (id) {
  
                  for (var i = 0; i &lt; flights.length; i++) {
                      if (flights[i].flightId == id) {
                          return flights[i];
                      }
                  }
                  return null;
              }
          };
  
      });
  </code></pre>
  
  <p>Here is how i get all the flights in the first controller</p>
  
  <pre><code>flightFactory.getFlights().then(function(response) {
                $scope.flights = response.data;
  });
  </code></pre>
  
  <p>Here is how I have been trying to get a single flight in the second controller</p>
  
  <pre><code>$scope.singleflight = flightFactory.getFlight($routeParams.flightID);
  </code></pre>
  
  <p>Here is the content of the JSON file</p>
  
  <pre><code>[
  {
      "date": "2016-02-18T00:19:11-08:00",
      "numberOfSeats": 30,
      "totalPrice": 610,
      "flightId": "PR6674",
      "travelTime": 422,
      "destination": "South Sudan",
      "origin": "Copenhagen"
  },
  {
      "date": "2016-11-07T16:35:45-08:00",
      "numberOfSeats": 87,
      "totalPrice": 405,
      "flightId": "ZN4890",
      "travelTime": 196,
      "destination": "Rwanda",
      "origin": "Copenhagen"
  },
  {
      "date": "2016-07-11T18:05:32-07:00",
      "numberOfSeats": 36,
      "totalPrice": 646,
      "flightId": "AT1096",
      "travelTime": 399,
      "destination": "Equatorial Guinea",
      "origin": "Copenhagen"
  }
  ]
  </code></pre>
  
  <p>Help will be much appreciated!</p>