[英]JavaScript testing: change Boolean value into an array of object
[英]javascript testing if value is in array
在javascript中是否有一个函数来执行此操作 - 测试一个给定的值(在我的情况下是一个字符串)是否存在于一个数组(字符串)中? 我知道我可以循环遍历每个值,但我想知道js是否有一个简写。
jquery有.inArray()方法。 这是我所知道的最好的......
这有.indexOf()
,但IE <9不支持它:
if(array.indexOf("mystring") > -1) {
//found
}
在同一个MDC文档页面中 ,这里包含(使用前)使IE工作的内容:
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
.indexOf()
返回找到字符串的数组中的索引,如果未找到则返回-1
。
对于php中的函数,应该是javascript或者我还没有找到,我去了: http : //phpjs.org
如果你只是使用javascript你想要使用的函数是in_array: http ://phpjs.org/functions/in_array: 432
否则,请在此页面上使用jQuery解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.