Filter NOT equals in AngularJS

前端 未结 3 1898
囚心锁ツ
囚心锁ツ 2020-12-21 04:55

I have a array of objects, in client side. The object in array look like this:

{
    code: 0,
    short_name: \'a\',
    type: 1
}

I try to

相关标签:
3条回答
  • 2020-12-21 05:15

    Again, if you are just going to filter, use the native method instead:

    $scope.array1 = data.filter(x => x.type === 1);
    $scope.array2 = data.filter(x => x.type !== 1);
    
    0 讨论(0)
  • 2020-12-21 05:16

    You're very close. You just need to change your second filter to:

    $scope.array2 = $filter('filter')(data, { type: '!1' });
    

    I also renamed the scope variable since otherwise it would just overwrite your first filtered array.

    0 讨论(0)
  • 2020-12-21 05:20

    For me the given solution { type: '!1' } doesn't work when used with true as last parameter $filter('filter')(data, {type: 1}, true);. However using a function works for me (see angular's doc):

    $scope.array2 = $filter('filter')(data, function(value, index, array) {
                return (value.type !== 1);
            }, true);
    
    0 讨论(0)
提交回复
热议问题