问题:
This question already has answers here :
这个问题已经在这里有了答案 :
How do I check if an array includes a value in JavaScript?
如何检查数组是否在JavaScript中包含值?
(47 answers)
(47个答案)
Closed 3 years ago .
3年前关闭。
I need to determine if a value exists in an array. 我需要确定数组中是否存在值。
I am using the following function: 我正在使用以下功能:
Array.prototype.contains = function(obj) {
var i = this.length;
while (i--) {
if (this[i] == obj) {
return true;
}
}
return false;
}
The above function always returns false. 上面的函数总是返回false。
The array values and the function call is as below: 数组值和函数调用如下:
arrValues = ["Sam","Great", "Sample", "High"]
alert(arrValues.contains("Sam"));
解决方案:
参考一: https://stackoom.com/question/4xNf/确定数组是否包含值-重复参考二: https://oldbug.net/q/4xNf/Determine-whether-an-array-contains-a-value-duplicate
来源:oschina
链接:https://my.oschina.net/stackoom/blog/4306921