简体   繁体   中英

How to filter with Array#filter()

I'm getting info from an API, and i want to filter some results:

This is my code to consume the API:

function listData() {
    $http.get('/api/Invoices?')
        .then(function(data) {
            $scope.list = data.data.Response;
    });
}

Then, with ng-repeat, i feed a list:

<tr ng-repeat="info in list">
                <th>{{info.Id}}</th>
                <th>{{info.Name}}</a></th>
                <th>{{info.value}}</th>
                <th>{{info.FiscalFolio}}</th>
</tr>

I want to filter for Id when the API is consumed. Someone sugest me to use Array#filter(), but i cannot make it work. This is my test, but i'm not sure is right:

function listData() {
    $http.get('/api/Invoices?')
        .then(function(data){
            $scope.list = data.data.Response;

            var pool = $scope.list;

            var ajax = pool.filter(function(xavier) {
                return xavier.StatusId === 1;                
            }); 
        });     
}

I've got two questions:

  1. Is correct the way i'm working with the filter?
  2. Did i need to put the variable on the html view?

Can you help me with an example?

filter() returns a new array. Just filter the Response array as you assign it to $scope.list if you don't care about any of the other data

$scope.list = data.data.Response.filter(function(xavier){
     return xavier.StatusId === 1;
}); 

If you need to store the Response array you can assign it to another variable for using another filter on it later on

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM