0

In the following callback function in my angular JS app, I want only the values to be added in array addresses[ ] that have the value of val in the funtion parameter. This is basically for the function to return values suggested based of string match from my json:

$scope.getAirports = function(val) {
    return $http.get('https://raw.githubusercontent.com/vedvasa/airports/master/airports.json', {
                              params: {
                                address: val,
                                sensor: false
                              }
                            }).then(function(res){

                              var addresses = [];
                              angular.forEach(res.data.records, function(item){
                              addresses.push(item.code);
                              });
                              return addresses;

                            });
                        };
jack
  • 77
  • 1
  • 7
  • Few things there API is taking in a parameter for val, what does key does val correspond to. Ideally this filtering should happen in the API itself. Other option is to use [filters](http://stackoverflow.com/a/17193427/452708) in then() Put your code on plnkr for easy reference https://plnkr.co/edit/GIApq3EbE0JvIT97BvG3 – Abhijeet Sep 25 '16 at 03:18
  • Possible duplicate of [How to filter JSON-Data with AngularJs?](http://stackoverflow.com/questions/11923142/how-to-filter-json-data-with-angularjs) – Abhijeet Sep 25 '16 at 03:20
  • @Abhijeet https://github.com/vedvasa/itinerary/blob/master/views/index.ejs – jack Sep 25 '16 at 03:41

1 Answers1

0

Use filter() in your array

fetch("https://raw.githubusercontent.com/vedvasa/airports/master/airports.json").
then(e => e.json()).
then(e => Promise.resolve(e.
    records.
    filter(e => e.carriers == 1))).
then(e => console.log(e.length))

Show this log 1655 at difference the 3885.

JonDotsoy
  • 104
  • 1
  • 4