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
But it doesn't happen to filter properly. See working plunkr here Any help will be welcome, thanks!