我有一个键值过滤器。
$scope.filter.subjects = [
{ Key: 27, Value: "Txt" }
{ Key: 19, Value: "Txt" }
{ Key: 7, Value: "Txt" }
{ Key: 5, Value: "Txt" }
]
我希望过滤另一个键值集合的结果集合。
result = [{
Prop1: "txt",
Prop2: "txt",
...
Subjects: [{
{ Key: 7, Value: "Txt" }
{ Key: 5, Value: "Txt" }
],
...
}]
这个lodash过滤目前正在运行,但看起来非常低效和笨重。我可以在后端更改过滤器和结果模型而不是键和值的集合我可以返回一个对象,其值映射如下:
...
Subjects: {
7: "Txt",
5: "txt"
}
...
并将过滤器更改为:
$scope.filter.subjects = {
27: "Txt",
19: "Txt",
7: "Txt",
5: "Txt"
}
如果这可以提高效率
以下是当前过滤器:
...
var filteredList = angular.copy($scope.data);
...
if ($scope.filter.Subject.length > 0) {
filteredList = _.filter(filteredList, function (o) {
return _.find(o.Subjects, function (y) {
return _.find($scope.filter.Subject, ["Key", y.Key]);
});
});
}
...
$scope.result = filteredList;
答案 0 :(得分:1)
虽然不确定你到底在想什么。如果您的$scope.filter.Subject
只是一个地图对象,例如{27: 'Txt', 12: '...'}
而不是[{key: '', value: ''}, {<key,value>}, ...}]
,那么您可以轻松跳过最内部的迭代,以便在给定示例中您正在尝试的内容中进行查找,并可以将代码更改为:
filteredList = _.filter(filteredList, function (o) {
return _.find(o.Subjects, function (y) {
return $scope.filter.subjects[y.Key];
});
});