How do I filter an array by a property that exists in another array?
I have this two arrays in my control:
this.items = [{ "Id": 1, "TypeId": 1, "name": "Michael" },
{ "Id": 2, "TypeId": 2, "name": "Helga" },
{ "Id": 3, "TypeId": 1, "name": "Max" },
{ "Id": 4, "TypeId": 2, "name": "Ann" }];
this.filterBy = [{ "Id": 1, "gender": "Male" },
{ "Id": 2, "gender": "female" }];
And here is my ng-repeat in view template:
<tr ng-repeat="item in list.items">
<td>
<div class="container-fluid">
<div class="row">
<div class="text-center">{{ item.name }}</div>
</div>
</div>
</td>
</tr>
Here is the generated view:
At some point I want to filter items array by gender to show in the view above only females or only males.
I tried to implement filter, like this:
(function () {
"use strict";
angular.module("sensorsData").filter('filterByGender', filterByGender);
function filterByGender() {
var result = [];
return function (items, filterBy) {
if (!items)
return;
if (!filterBy)
return;
angular.forEach(items, function (item) {
angular.forEach(filterBy, function (f) {
if (item.TypeId == f.Id) {
result.push(item);
}
})
});
return result;
}
};
})();
And here how I use it in view template:
<tr ng-repeat="item in list.items | filterByGender:list.filterBy">
<td>
<div class="container-fluid">
<div class="row">
<div class="text-center">{{ item.name }}</div>
</div>
</div>
</td>
</tr>
The desired view in case if content of filterBy array like that:
this.filterBy = [{ "Id": 1, "gender": "Male" }];
The view Should display only items rows with TypeId = 1.
Like this:
But the filter above is not working.
And in the debugger I get this errors:
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: r in list.arr | filterByAlert:list.filterBy, Duplicate key: object:8, Duplicate value: {"Id":1,"TypeId":1,"name":"Michael"}
http://errors.angularjs.org/1.5.3/ngRepeat/dupes?p0=r%20in%20list.arr%20%7C…%3A8&p2=%7B%22Id%22%3A1%2C%22TypeId%22%3A1%2C%22name%22%3A%22Michael%22%7D
at angular.js:68
at ngRepeatAction (angular.js:28799)
at $watchCollectionAction (angular.js:16734)
at Scope.$digest (angular.js:16869)
at Scope.$apply (angular.js:17133)
at done (angular.js:11454)
at completeRequest (angular.js:11652)
at XMLHttpRequest.requestLoaded (angular.js:11593)
Why is the filter not working? And how can I create a filter to show items by gender?
And this:
angular.js:13424 Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
http://errors.angularjs.org/1.5.3/$rootScope/infdig?p0=10&p1=%5B%5D
at angular.js:68
at Scope.$digest (angular.js:16907)
at Scope.$apply (angular.js:17133)
at done (angular.js:11454)
at completeRequest (angular.js:11652)
at XMLHttpRequest.requestLoaded (angular.js:11593)(anonymous function) @ angular.js:13424
angular.js:17136 Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
http://errors.angularjs.org/1.5.3/$rootScope/infdig?p0=10&p1=%5B%5D
And this:
Uncaught Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
Why is the filter not working? And how can I create a filter to show items by gender?


typeId? Or that you've separated out what seems like relevant data into another variable?