本文介绍了如何使用AngularJS检查值是否在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种AngularJS检查数组中是否存在值的方法
is there an AngularJS way of checking if a value exists in an array
var array1 = ["a","b","c"]
我正在尝试这样做..
i'm trying to do this..
var array2 = ["c", "d", "e"]
angular.forEach(array2, function (a) {
if (a /*is NOT in array1*/) {
array1.push(a);
} else {
return false
}
});
推荐答案
您可以使用将返回 -1
如果找不到或者数组中的值的索引。
You can use Array.indexOf
which will return -1
if it's not found or the index of the value in the array.
那么在你的情况下:
if (array2.indexOf(a) < 0) {
array1.push(a);
}
这篇关于如何使用AngularJS检查值是否在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!