-1

This question comes from this one

In Javascript, I have this code in order to make a filtering, so that I can show the elements from the array colors just when they also appear as a value in cars

var colors = ["blue","red","pink","yellow"];

var cars = [{"brand":"Ford","color":"blue"}
           ,{"brand":"Ferrari","color":"red"}
           ,{"brand":"Rolls","color":"blue"}];

var filteredColors = colors.filter(function(color) { 
    return cars.some(function(car) { 
        return car.color === color;
    }); 
});

console.log(filteredColors);

I have tried to apply this in Angular JS, doing this:

$scope.colors =["blue","red","pink","yellow"];

$scope.cars=[ {"brand":"Ford","color":"blue"}
           ,{"brand":"Ferrari","color":"red"}
           ,{"brand":"Rolls","color":"blue"}];

$scope.filteredColors = function() {
    var colorsvar = $scope.colors;
    var carsvar = $scope.cars;
    return colorsvar.filter(function(color) { 
        return carsvar.some(function(car) { 
            return car.color === color;
        });
    });

};

And then in the view:

<ul>
    <li ng-repeat="n in colors | filter: filteredColors"> {{n}}
    </li>
</ul>

The result should be

  • blue
  • red
  • But it doesn't happen to filter properly. See working plunkr here Any help will be welcome, thanks!

    2 Answers 2

    1

    You can use custom filter as below, check the DEMO

    DEMO

    var app = angular.module("myApp",[]);
    app.controller("myCtrl",function($scope){
    $scope.colors =["blue","red","pink","yellow"];
    
    $scope.cars=[ {"brand":"Ford","color":"blue"}
               ,{"brand":"Ferrari","color":"red"}
               ,{"brand":"Rolls","color":"blue"}];
    
     
    })
    app.filter('myFilter', function() {
        // the filter takes an additional input filterIDs
        return function( filterColors, inputArray) {
            var filtered = [];
           angular.forEach(inputArray,function(key,value){
                 if(filterColors.includes(key.color) && !filtered.includes(key.color)) {
                    filtered.push(key.color);
                 }
           })
           return filtered;
        };
    });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    
    <div ng-app="myApp" ng-controller="myCtrl">
    <ul>
        <li ng-repeat="n in colors | myFilter: cars  track by $index"> {{n}}
        </li>
    </ul>
    </div>

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

    2 Comments

    Sorry, I had a typo in the question, the ng-repeat should be "n in colors", and it should yield as the result "blue" and "red" (no "blue", "red", "blue")
    Good Explination
    1

    No need to call a filter. just call the function inside the ng-repeat

     <li ng-repeat="n in filteredColors()"> {{n}}
     </li>
    

    Demo

    var app = angular.module("myApp",[]);
    app.controller("myCtrl",function($scope){
    $scope.colors =["blue","red","pink","yellow"];
    
    $scope.cars=[ {"brand":"Ford","color":"blue"}
               ,{"brand":"Ferrari","color":"red"}
               ,{"brand":"Rolls","color":"blue"}];
    
    $scope.filteredColors = function() {
        var colorsvar = $scope.colors;
        var carsvar = $scope.cars;
        return colorsvar.filter(function(color) { 
            return carsvar.some(function(car) { 
                return car.color === color;
            });
        });
    
    };
    })
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    
    <div ng-app="myApp" ng-controller="myCtrl">
    <ul>
        <li ng-repeat="n in filteredColors()"> {{n}}
        </li>
    </ul>
    </div>

    2 Comments

    Is there it any way to apply it as a filter? (I think I need that approach for my app) If not I will mark it as correct anyway. Thanks!

    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.