"Typetwo" ] 如何让过滤器迭代整个数组并匹配与值匹配的结果? 使用自定义过滤-6ren">
gpt4 book ai didi

javascript - 使用各种数组或对象项过滤 ng-repeat?

转载 作者:行者123 更新时间:2023-12-03 12:38:09 25 4
gpt4 key购买 nike

好吧,我有一个 ng-repeat:

<div id="scrolltwo" class="animate-repeat panel" ng-repeat="items in arrayfull |filter:filtertype ">

在过滤器类型中我有:

[[0] => "Typeone", [1] => "Typetwo" ]

如何让过滤器迭代整个数组并匹配与值匹配的结果?

使用自定义过滤器更新:

<div id="scrolltwo" class="animate-repeat panel" ng-repeat="items in arrayfull | array | filter:matchaccnttypes(accnttypes)">

有效的数组过滤器:

.filter('array', function() {
return function(items) {
console.log(items);
var filtered = [];
angular.forEach(items, function(item) {
filtered.push(item);
});
return filtered;
};
})

匹配帐户类型,但不匹配!:

                    $scope.matchaccnttypes = function matchaccnttypes(query) {
return function(items) {
return items.Organtype.match(query);
angular.forEach($scope.accnttypes, function(value, key){
console.log(key + ': ' + value);
});
}
};

它只是返回传递到 $scope.accntypes 数组中的最后一个值,这是我上面的示例数组..

最佳答案

借助 underscoreJS 的自定义过滤器。这是Fiddle

app.filter('arrayFilter', function() {
return function(input, filterType) {
return _.intersection(input,filterType);
};
});

没有 UnderscoreJS

app.filter('arrayFilter', function() {
return function(input, filterType) {
var filterdArray =[];
angular.forEach(input, function(inputValue, key){
angular.forEach(filterType, function(filterTypeValue, key){
if(inputValue==filterTypeValue){
filterdArray.push(inputValue);
}
});

});
return filterdArray;

};
});

ng-repeat应按以下方式使用。

ng-repeat="array in arrayfull | arrayFilter : filterType"

关于javascript - 使用各种数组或对象项过滤 ng-repeat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23638022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com