是否有一种AngularJS方法来检查数组中是否存在值
var array1 = ["a","b","c"]
我试图这样做..
var array2 = ["c", "d", "e"]
angular.forEach(array2, function (a) {
if (a /*is NOT in array1*/) {
array1.push(a);
} else {
return false
}
});
答案 0 :(得分:28)
答案 1 :(得分:10)
您只需使用原生Array.prototype.indexOf来检查值是否在数组中:
var array2 = ["c", "d", "e"]
angular.forEach(array2, function (a) {
if (array2.indexOf(a) === -1) {
// a is NOT in array1
array1.push(a);
}
});
答案 2 :(得分:0)
https://www.w3schools.com/jsref/jsref_includes_array.asp
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var n = fruits.includes("Mango");