1

In Angular, I have an array like this:

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

And another object

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

I would like to do filter, so that

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

The ng-repeat would just display the elements in the $scope.colors that exist as values in the $scope.cars

So, in other words, it would just display blue and red

Thanks in advance!

2
  • 1
    Your second array is invalid Commented Jan 5, 2018 at 12:15
  • True that. Fixed! Commented Jan 5, 2018 at 12:22

2 Answers 2

1

Given colors and cars arrays, you can filter colors by:

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

var filteredColors = colors.filter(color => cars.some(car => car.color === color));

console.log(filteredColors);

If you cannot use ES6, it should be:

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);

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

1 Comment

Thanks, would it be possible to do it without ES6 expressions? (for SEO reasons-as for now googlebot doesn't accept ES6) If not I will mark it as valid
1

You can use map method which accepts as parameter a callback function in order to obtain all the colours from each car item from $scope.cars, and then use Set constructor in order to find out unique colours. Then you have to use a filter function.

Then you have to use a filter function.

let colors =["blue","red","pink","yellow"];
let cars = [{"brand":"Ford","color":"blue"},{"brand":"Ferrari","color":"red"},{"brand":"Rolls","color":"blue"}];
let uniqueColours= [...new Set(cars.map(c => c.color))];
const filteredColors = colors.filter(a=>uniqueColours.includes(a));
console.log(filteredColors);

You can do this in a single line:

const colors =["blue","red","pink","yellow"],cars = [{"brand":"Ford","color":"blue"},{"brand":"Ferrari","color":"red"},{"brand":"Rolls","color":"blue"}],
filteredColors = colors.filter(a => cars.map(a => a.color).includes(a));
console.log(filteredColors);

Without arrow functions.

const colors =["blue","red","pink","yellow"],cars = [{"brand":"Ford","color":"blue"},{"brand":"Ferrari","color":"red"},{"brand":"Rolls","color":"blue"}],
filteredColors = colors.filter(function(a){ 
    return cars.map(function(c){
       return c.color;
    }).includes(a)});
console.log(filteredColors);

2 Comments

Thanks, I think both answers are valid, I will mark @Faly as correct as he was the first to answer, but for angular it would be optimal if the solution would not have ES6 expressions (as for today googlebot breaks with ES6)
@Joe82, i added a solution without ES6

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.