1

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

2 Answers 2

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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

  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.