Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将字符串与数组中的值匹配,并在JavaScript中返回该值的id_Javascript_Arrays - Fatal编程技术网

将字符串与数组中的值匹配,并在JavaScript中返回该值的id

将字符串与数组中的值匹配,并在JavaScript中返回该值的id,javascript,arrays,Javascript,Arrays,假设我有以下JavaScript数组: var skins = new Array('Light', 'Medium', 'Dark'); 如何检查数组(0、1或2)中的哪个ID与我给定的字符串具有匹配值。例如,如果我查看一个“Medium”字符串,则应返回ID 1。您可以使用: 此函数已在中引入,但为了与旧浏览器兼容,可以将其包括在内 以下是Firefox使用的算法: if (!Array.prototype.indexOf) { Array.prototype.indexOf = fu

假设我有以下JavaScript数组:

var skins = new Array('Light', 'Medium', 'Dark');
如何检查数组(0、1或2)中的哪个ID与我给定的字符串具有匹配值。例如,如果我查看一个“Medium”字符串,则应返回ID 1。

您可以使用:

此函数已在中引入,但为了与旧浏览器兼容,可以将其包括在内

以下是Firefox使用的算法:

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;
  };
}
if(!Array.prototype.indexOf){
Array.prototype.indexOf=函数(elt/*,from*/){
var len=this.length>>>0;
var from=Number(参数[1])| | 0;
from=(from<0)
?数学单元(来自)
:数学。地板(从);
如果(从<0开始)
from+=len;
for(;from
Array.prototype.lastIndex=函数(什么){
var L=此长度;
while(L){
如果(this[--L]==what)返回L;
}
返回-1;
}
Array.prototype.firstIndex=函数(什么){
var i=0,L=此长度;
我看一下:
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;
  };
}
Array.prototype.lastIndex= function(what){
 var L= this.length;
 while(L){
  if(this[--L]=== what) return L;
 }
 return -1;
}

Array.prototype.firstIndex= function(what){
 var i=0, L= this.length;
 while(i<L){
  if(this[i]=== what) return i;
  ++i;
 }
 return -1;
}