栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

Javascript Array.lastIndexOf()方法

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Javascript Array.lastIndexOf()方法

array.lastIndexOf(searchElement[, fromIndex]);

下面是参数的详细信息:

  • searchElement : 定位数组中的元素

  • fromIndex : 索引在 start 倒退搜索。默认为数组的长度,即整个数组将被搜索。如果该指数大于或等于该数组的长度,整个数组将被搜索。如果为负,它被作为从数组的端部的偏移量。

返回值:

返回从最后找到元素的索引

兼容性:

这种方法是一个Javascript扩展到ECMA-262标准;因此它可能不存在在标准的其他实现。为了使它工作,你需要添加下面的脚本代码在顶部:

if (!Array.prototype.lastIndexOf)
{
  Array.prototype.lastIndexOf = function(elt )
  {
    var len = this.length;

    var from = Number(arguments[1]);
    if (isNaN(from))
    {
      from = len - 1;
    }
    else
    {
      from = (from < 0)
    ? Math.ceil(from)
    : Math.floor(from);
      if (from < 0)
 from += len;
      else if (from >= len)
 from = len - 1;
    }

    for (; from > -1; from--)
    {
      if (from in this &&
   this[from] === elt)
 return from;
    }
    return -1;
  };
}
例子:


Javascript Array lastIndexOf Method




这将产生以下结果:

index is : 2
index is : 5  

 


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/10007.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号