AngularJS - 如何检查特定键在数组中是否具有特定值 [英] AngularJS - How to check that a specific key has a specific value in an array
本文介绍了AngularJS - 如何检查特定键在数组中是否具有特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Angular中,我有一个这样的数组:
In Angular, I have an array like this:
$scope.colors =["blue","red","pink","yellow"];
另一个对象
$scope.cars=[{"brand":"Ford","color":"blue"},{"brand":"Ferrari","color":"red"},{"brand":"Rolls","color":"blue"}];
我想做过滤,所以
<ul>
<li ng-repeat="n in colors | filter:colorFilter">
</li>
</ul>
ng-repeat只显示 $ scope.colors中的元素
作为 $ scope.cars
中的值存在
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
提前致谢!
推荐答案
鉴于 colors 和 cars 数组,您可以通过以下方式过滤 colors :
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);
如果你不能使用ES6,它应该是:
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);
这篇关于AngularJS - 如何检查特定键在数组中是否具有特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文