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

This Angular filter is being passed an array of objects used in an ng-repeat (item in items).

    [{
        title: "Star Wars",
        categories: ["Adventure", "Family"]
    }, {
        title: "Star Search",
        categories: ["Realty", "Fantasy"]
    }, {
        title: "Star Trek",
        categories: ["Sci-Fi", "Fantasy"]
    }]


And an array of strings (categoriesFiltered)

    ["Adventure", "Sci-Fi"]


What would be the best way to iterate through the items in the ng-repeat and compare the categories array... to the categoriesFiltered array, and push the items that match to the new filtered array?


This is how far I have come

categoryFilter.filter('filterCategories', function () {
    return function (items, categoriesFiltered) {
        var filtered = [];

        for (var i = 0; i < items.length; i++) {
            for (var j = 0; j < items[i].categories; j++) {
                if (filtered.indexOf(items[i].categories[j]) === -1) {
                    filtered.push(items[i]);
                };
            };
        };
        return filtered;
    };
});

Here is a plunker...

share|improve this question
    
Approach appears reasonable...is it working? Some might use Array.prototype.filter() or Array.prototype.map() – charlietfl Jul 27 at 0:56
    
I will look into those directions... Thank you @charlietfl – John Spiteri Jul 27 at 1:04
    
no real need if it works...for loops work well...is there a problem or not? If this is just a code review question is better to ask those on codereview.stackexchange.com Questions here are meant for real problems – charlietfl Jul 27 at 1:15
    
Apologize @charlietfl - I did not answer your question, the for loops are not working properly – John Spiteri Jul 27 at 2:05
    
can you put a simple demo together for this in plunker? Doesn't need any css or anything fancy – charlietfl Jul 27 at 2:05

1 Answer 1

up vote 0 down vote accepted

There are a few problems.

  1. Your returned filtered array falls outside the returned function, it should be inside.
  2. You're not checking for items[i].categories.length in your for loop condition.
  3. You're using the wrong array to check the indexOf.
  4. I'm assuming your categoriesFiltered example was a typo looks like ["Adventure", "Sci-Fi"] because [{"Adventure", "Sci-Fi"}] is invalid javascript.

Try this:

categoryFilter.filter('filterCategories', function () {
    return function (items, categoriesFiltered) {
        var filtered = [];

        for (var i = 0; i < items.length; i++) {
            for (var j = 0; j < items[i].categories.length; j++) {
              if (categoriesFiltered.indexOf(items[i].categories[j]) > -1) {
                filtered.push(items[i]);
              }
            }
        }
        return filtered;
    };
});
share|improve this answer
    
1. Yes it is outside... edited. 2. Checking with logging. 3. Checking with Google. 4. Yes it was a typo... edited. Thank you @user2341963 – John Spiteri Jul 27 at 11:24
    
Yes @user2341963... I see where 2 and 3 were wrong. The filter is working now, nicely put - Thank you – John Spiteri Jul 27 at 14:16

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.