[英]Angular dynamic filter for array
I'm trying to create a filtered array with dynamic variables. 我正在尝试使用动态变量创建一个过滤后的数组。 I create an array which holdes the filter keys, and then I create an filtered array which only should return items that match the keys from my first array.
我创建了一个保存过滤键的数组,然后创建了一个过滤后的数组,该数组仅应返回与第一个数组中的键相匹配的项目。
Array with filter keys: $scope.participantArray = ["kti@test.com", "mob@test.com"]
带有过滤键的数组:
$scope.participantArray = ["kti@test.com", "mob@test.com"]
Code for filtering second array: 用于过滤第二个数组的代码:
$scope.items = $scope.items.filter(function (data) {
var i = $scope.participantArray.length;
while( i-- ) {
return ( data.Title === $scope.participantArray[i] )
}
I'm trying to loop through all keys and apply them to the filtered array. 我试图遍历所有键并将它们应用于过滤后的数组。 The problem is that it only returns one match.
问题在于它仅返回一个匹配项。 I ahve to instances in my
items
array which match the keys from my first array. 我要在我的
items
数组中匹配与第一个数组中的键匹配的实例。
The while loop only returns mob@test.com
. while循环仅返回
mob@test.com
。
Any suggestions on what i am doing wrong? 关于我在做什么错的任何建议?
You can do it in an easier way using indexOf
: 您可以使用
indexOf
以更简单的方式进行操作:
$scope.items.filter(function(item) {
if($scope.participantArray.indexOf(item.Title) >= 0) {
return true;
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.