2016-09-25 56 views
0

在我的角JS应用程序中的以下回调函数中,我只想将值添加到数组地址[]中,该值在函数参数中具有值val。 这基本上是对返回值的功能提示基于字符串匹配的从我的JSON:想要过滤来自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; 

          }); 
         }; 
+0

API中有一些API正在参数val中,key与val对应的是什么。理想情况下,这种过滤应该发生在API本身。 其他选项是在()中使用[filters](http://stackoverflow.com/a/17193427/452708) 将您的代码放在plnkr上以便于参考https://plnkr.co/edit/GIApq3EbE0JvIT97BvG3 – Abhijeet

+0

可能[如何使用AngularJs过滤JSON-Data?](http://stackoverflow.com/questions/11923142/how-to-filter-json-data-with-angularjs) – Abhijeet

+0

@Abhijeet https:// github。 COM/vedvasa /行程/斑点/主/视图/ index.ejs – jack

回答

0

使用filter()在你的阵列

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)) 

显示此日志1655在差异3885