I need to filter an array of objects by another array of objects, which I am finding more difficult than I thought it would be.
$scope.ArrayMain = [{
"LocationID": 1,
"AdornmentID": 11,
"ApplicabilityID": 111
},{
"LocationID": 2,
"AdornmentID": 22,
"ApplicabilityID": 222
},{
"LocationID": 3,
"AdornmentID": 33,
"ApplicabilityID": 333
},{
"LocationID": 4,
"AdornmentID": 44,
"ApplicabilityID": 444
}];
var FiterByArray = [{
"filterID": 1,
"ApplicabilityID": 222
},{
"FilterID": 2,
"ApplicabilityID": 444
}];
I need to filter by the applicabilityID and return something similar to what is below...
$scope.filteredList = [{
"LocationID": 1,
"AdornmentID": 11,
"ApplicabilityID": 111
},{
"LocationID": 3,
"AdornmentID": 33,
"ApplicabilityID": 333
}]
If that array was simpler I could do it this way..
var notWhatIWant = [ 222, 444 ];
$scope.FiterByArray = function(e) {
return FiterByArray.indexOf(e.ApplicabilityID) === -1;
}
<div ng-repeat="a in ArrayMain | filter: FiterByArray">
{{ a }}
</div>
This doesn't work with the FiterByArray. I have considered creating a function that loops through the FilterByArray and removed just the ApplicabilityID and creates a new array to filter against, but not sure that is the correct approach for this.
This doesn't work but here is the fiddler I setup to experiment on JSFiddler
Try this filter https://jsfiddle.net/17jtu5r5/5/
angular.module('myApp', [])
.controller('myCtrlr', ['$scope', function($scope) {
$scope.ArrayMain = [{
"LocationID": 1,
"AdornmentID": 11,
"ApplicabilityID": 111
}, {
"LocationID": 2,
"AdornmentID": 22,
"ApplicabilityID": 222
}, {
"LocationID": 3,
"AdornmentID": 33,
"ApplicabilityID": 333
}, {
"LocationID": 4,
"AdornmentID": 44,
"ApplicabilityID": 444
}];
/*
var FiterByArray = [ 222, 444 ];
*/
var FiterByArray = [ {
"filterID": 1,
"ApplicabilityID": 222
},{
"FilterID": 2,
"ApplicabilityID": 444
}];
}]).filter('myFilter',function(){
return function (items) {
var obj = items.filter(function(x){
return x.ApplicabilityID === 111
});
return obj;
}
});
By pure JS you may do as follows;
var ArrayMain = [{ "LocationID": 1, "AdornmentID": 11, "ApplicabilityID": 111 }, { "LocationID": 2, "AdornmentID": 22, "ApplicabilityID": 222 }, { "LocationID": 3, "AdornmentID": 33, "ApplicabilityID": 333 }, { "LocationID": 4, "AdornmentID": 44, "ApplicabilityID": 444 } ], FiterByArray = [{ "filterID": 1, "ApplicabilityID": 222}, { "filterID": 2, "ApplicabilityID": 444} ], resultArray = ArrayMain.filter(e => !FiterByArray.some(f => f.ApplicabilityID === e.ApplicabilityID)); console.log(resultArray)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.