检查元素是否是一个数组present
问题描述:
我现在使用的检查这个函数如下:
The function I am using now to check this is the following:
function inArray(needle,haystack)
{
var count=haystack.length;
for(var i=0;i<count;i++)
{
if(haystack[i]===needle){return true;}
}
return false;
}
它的工作原理。我正在寻找的是是否有这样做的更好的方法。
It works. What I'm looking for is whether there is a better way of doing this.
答
code:
function isInArray(value, array) {
return array.indexOf(value) > -1;
}
执行:
isInArray(1, [1,2,3]); // true