2

This questions comes from here

I want to make a filtering, so that I can show the values of colors.name just when they also appear as a value in cars.color

$scope.colors = [{"name":"blue","count":2},
                 {"name":"red","count":12},
                 {"name":"pink","count":5},
                 {"name":"yellow","count":2}];

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

And then in the view:

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

The result should be

  • blue
  • red
  • I need the answer not to have ES6, and I need the filter to be in the controller. See plunkr here. Thanks in advance!

    2
    • There are differences in the plnkr and your example. Are they intentional, if they are, which one is the source of truth? Commented Jan 10, 2018 at 15:03
    • Yep, sorry, updated the plunkr now. Commented Jan 10, 2018 at 16:45

    3 Answers 3

    1
    +50

    controller:

    var app = angular.module('plunker', []);
    
    app.controller('MainCtrl', function($scope) {
    
      $scope.colors = [{"name":"blue","count":2},
                     {"name":"red","count":12},
                     {"name":"pink","count":5},
                     {"name":"yellow","count":2}];
    
      $scope.cars=[ {"brand":"Ford","color":"blue", "seat":"pink"}
           ,{"brand":"Ferrari","color":"red", "seat":"pink"}
           ,{"brand":"Rolls","color":"blue","seat":"pink"}];
    
      $scope.filteredColors = function(color) {
        var carsvar = $scope.cars;
        for(var x = 0; x < carsvar.length; x++) {
          if (color.name === carsvar[x].color) {
            return true;
          }
        }
        return false; 
      };
    
    });
    

    view:

    <!DOCTYPE html>
    <html ng-app="plunker">
    
      <head>
        <meta charset="utf-8" />
        <title>AngularJS Plunker</title>
        <link rel="stylesheet" href="style.css" />
        <script data-require="[email protected]" src="https://code.angularjs.org/1.6.6/angular.js" data-semver="1.6.6"></script>
        <script src="app.js"></script>
      </head>
    
      <body ng-controller="MainCtrl">
        <ul>
            <li ng-repeat="color in colors | filter: filteredColors"> {{color.name}}
            </li>
        </ul>
      </body>
    
    </html>
    
    Sign up to request clarification or add additional context in comments.

    3 Comments

    btw $scope.colors, cars in your plunker don't match your question, but I just copied them over to plunker and works fine :)
    Hi, thanks for your answer, but for my user case, I need a filter that could be applied to the ng-repeat (not an "already filtered" scope).
    @Joe82, ah, finally understood you. I've updated my answer which now produces * blue * red using a filter.
    1

    You can use a custom filter:

     app.filter('colorFilter', function(){
        return function(val){
            var colorsvar = [{"name":"blue","count":2},
                 {"name":"red","count":12},
                 {"name":"pink","count":5},
                 {"name":"yellow","count":2}];
            var filtered = []
            angular.forEach(val, function(key, value){
                    angular.forEach(colorsvar, function(key2, value2){
                         if(key.color === key2.name)
                             filtered.push(key)
                    })
            })
            return filtered;
        }
    })
    

    And then on your html:

    <li ng-repeat="n in cars | colorFilter"> {{n.color}}
    

    Hope this helps.

    1 Comment

    Thanks for the insight, but I couldn't make it work in my plunkr. I would as well need the filter inside the controller.
    0

    As per your first question, there have some answers without using ES6. AngularJS - How to check that a specific key has a specific value in an array

    So I thought you don't need to use any inbuilt function to do your logic.

    Try to use with manual loop instead of using map or include bla bla bla.. Do Just like a normal JavaScript way in angularjs. Get this answer as a key. Take this answer as a key and do it with angular.filter().

    var app = angular.module("myApp",[]);
    app.controller("myCtrl",function($scope){
    $scope.colors  = [{"name":"blue","count":2},
                 {"name":"red","count":12},
                 {"name":"pink","count":5},
                 {"name":"yellow","count":2}];
    
    
    
    $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;
    $scope.newValue = [];
    for (var i = 0; i < colorsvar.length; i++) {
        for (var j = 0; j < carsvar.length; j++) {
            if (colorsvar[i].name == carsvar[j].color) {
                if ($scope.newValue.indexOf(colorsvar[i].name) ==-1) {
                    $scope.newValue.push(colorsvar[i].name);
                }
            }
        }
    }
       return $scope.newValue;
      };
     })
    <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

    Thanks for your comments!
    I didn't comment here.

    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.