簡體   English   中英

如何使用Array#filter()進行過濾

[英]How to filter with Array#filter()

我從API獲取信息,我想過濾一些結果:

這是我使用API​​的代碼:

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

然后,使用ng-repeat,我輸入一個列表:

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

我想在使用API​​時過濾ID。 有人建議我使用Array#filter(),但我無法使其工作。 這是我的測試,但我不確定是否正確:

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

我有兩個問題:

  1. 我使用過濾器的方式正確嗎?
  2. 我是否需要將變量放在html視圖上?

你能幫我舉個例子嗎?

filter()返回一個新數組。 如果您不關心其他任何數據,只需將Response數組分配給$scope.list對其進行過濾

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

如果您需要存儲Response數組,可以將其分配給另一個變量,以便稍后使用另一個過濾器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM