Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm trying to filter a nested array of objects with my own objects, by idSubject. But I'm not getting the right result.

I have articles (which have subjects) And a array of objects (which are the subjects I want to filter the articles with)

Data looks like this:

enter image description here

enter image description here

So I'm trying to filter the array of articles by its subjects.

I tried the following:

<div class="panel panel-default"
                     ng-repeat="searchArticle in searchArticles | filter: {subjects: filterSubjects} as articleSearchResult">

So filterSubjects is the second screenshot and SearchArticles is the first screenshot.

Without much luck.

Hope you can help, please tell me if things are still unclear.

share|improve this question
    
You will need to create a custom filter for this and do the matching yourself – charlietfl 8 hours ago
    
@charlietfl Any example?. I tried this a couple of times but failed. – TVH7 8 hours ago
    
Are all the name values unique? If so I would make an object that uses those values as keys and then in the filtering check if that property exists when you iterate main array – charlietfl 8 hours ago
    
@charlietfl No. Only the idSubject. This is also the only property I want to filter. There are checkboxes which I want to be able to check and then filter the subjects of the articles. The idSubject is unique. – TVH7 8 hours ago
    
Same principle then ... extract all the ID's from filterSubjects then check when you iterate searchArticles – charlietfl 8 hours ago

This custom filter will help you.

Example : http://plnkr.co/edit/jMizCLxPH6DtDA5wL15Q?p=preview

HTML:

<body ng-app="myApp">
  <div ng-controller="MainCtrl">
<h2>Select Subjects</h2>
    <div ng-repeat="subject in subjects">
      <label>
        <input type="checkbox" ng-model="filterSubjects[subject.id]" ng-true-value="'{{subject.id}}'" ng-false-value="''">{{subject.name}}</label>
    </div>

    <h2>Filtered Articles</h2>

    <div ng-repeat="searchArticle in searchArticles | subjectFilter:filterSubjects">{{searchArticle.name}}</div>
  </div>
</body>

JS:

var app = angular.module('myApp', []);

app.controller('MainCtrl', function($scope) {
  $scope.searchArticles = [{
    "name": "Article1",
    "sid": "1"
  }, {
    "name": "Article2",
    "sid": "1"
  }, {
    "name": "Article3",
    "sid": "2"
  }];
  $scope.subjects = [{
    "name": "Subject1",
    "id": "1"
  }, {
    "name": "Subject2",
    "id": "2"
  }];
  $scope.filterSubjects = [];

});

app.filter('subjectFilter', function() {
  return function(articles, filterSubjects) {
    filtered = articles.filter(function(e){return filterSubjects.indexOf(e.sid) >= 0},filterSubjects);
    return filtered;
  }
});

if you want to filter based on object :

var app = angular.module('myApp', []);

app.controller('MainCtrl', function($scope) {
  $scope.searchArticles = [{
    "name": "Article1",
    "sid": "1"
  }, {
    "name": "Article2",
    "sid": "1"
  }, {
    "name": "Article3",
    "sid": "2"
  }];
  $scope.subjects = [{
    "name": "Subject1",
    "id": "1"
  }, {
    "name": "Subject2",
    "id": "2"
  }];
  $scope.filterSubjects = [{
    "name": "Subject1",
    "id": "1"
  }, {
    "name": "Subject1",
    "id": "2"
  }];

});

app.filter('subjectFilter', function() {
  return function(articles, filterSubjects) {
    var sFiltered = [];
    for (var i = 0; i < filterSubjects.length; i++) {
      sFiltered.push(filterSubjects[i].id);
    }
    var filtered = articles.filter(function(e) {
      return sFiltered.indexOf(e.sid) >= 0;
    }, sFiltered);
    return filtered;
  }
});
share|improve this answer
    
It gets me in the right direction, but your data modal has a sid and not an array of objects like mine. How would I do it with the array of objects? Thanks for replying. – TVH7 7 hours ago
    
@TVH7 you are telling about filterSubjects will be array of object , if it is like this you want to write the loop to find the matching one. – sreeramu 6 hours ago
    
filterSubjects is indeed an array of 'subjects', an article has an array of subject objects too. So I want to filter articles by the array of subjects. Lets say I select subject 1 and 2, then I only want to see articles with subject 1 and 2 in its array of objects. – TVH7 6 hours ago
    
@TVH7 i had updated my answer for your object filter condition, if it is not helping, you can create one example show your problem so i can help you out. – sreeramu 6 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.