当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Filter AngularJS Array by another Array

问题描述:

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

网友答案:

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)
网友答案:

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;
        }

  });
分享给朋友:
您可能感兴趣的文章:
随机阅读: