简体   繁体   English

AngularJS如何过滤数组中的元素

[英]AngularJS how to filter an element in an array

I have this massive 我有这么大

$scope.arrVaritions = [
    [1, 2, 3], 
    [4, 5, 6], 
    [7, 8, 9], 
    [1, 4, 7], 
    [2, 5, 8], 
    [3, 6, 9], 
    [1, 5, 9], 
    [3, 5, 7]
];

How can I find an element index in this array? 如何在此数组中找到元素索引? I'm use this code, but it does not work: 我正在使用此代码,但是它不起作用:

$scope.elementId = 1;    
$filter('filter')($scope.arrVaritions, { $scope.elementId });

Try filter it in the right way like in this demo fiddle : 尝试像下面的演示小提琴一样以正确的方式对其进行过滤:

View 视图

<div ng-controller="MyCtrl">
  {{filtered}}
</div>

AngularJS application AngularJS应用

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope, $filter) {
    $scope.arrVaritions = [
        [1, 2, 3], 
        [4, 5, 6], 
        [7, 8, 9], 
        [1, 4, 7], 
        [2, 5, 8], 
        [3, 6, 9], 
        [1, 5, 9], 
        [3, 5, 7]
    ];

    $scope.elementId = 1;
    $scope.filtered = $filter('filter')($scope.arrVaritions, $scope.elementId, true);
});

If you want to filter not the exact value just remove the 3rd true option: 如果您不想过滤确切的值,只需删除第三个true选项:

$scope.filtered = $filter('filter')($scope.arrVaritions, $scope.elementId);

It should have only $scope.elementId instead of object, and 3rd option true to match exact value. 它应该只有$scope.elementId而不是object,并且第三个选项true才能匹配精确值。 Otherwise without true if elementId will consider 10 , 11 , 100 , etc. values (it will do contains check). 否则,没有true ,如果elementId会考虑1011100 ,等值(它会做包含检查)。

$filter('filter')($scope.arrVaritions, $scope.elementId, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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