zoukankan      html  css  js  c++  java
  • lastIndexOf() 找出指定元素出现的所有位置(返回的是下标数组)---lastIndexOf() 这个方法是倒叙查找,正序的是indexOf()

     1 var indices = [];
     2 var array = ['a', 'b', 'a', 'c', 'a', 'd'];
     3 var element = 'a';
     4 var idx = array.lastIndexOf(element);
     5 
     6 while (idx != -1) {
     7   indices.push(idx);
     8   idx = (idx > 0 ? array.lastIndexOf(element, idx - 1) : -1);
     9 }
    10 
    11 console.log(indices);
    12 // [4, 2, 0];

    上例使用 lastIndexOf 查找到一个元素在数组中所有的索引(下标),并使用 push 将所有添加到另一个数组中。

    注意,必须单独处理 idx == 0 时的情况,因为如果元素是数组中的第一个元素,则总会被查找,忽略了 fromIndex 参数。 这点和 indexOf 方法不同。(译注:个人觉得这句话解释有问题,idx == 0 时,array.lastIndexOf(element, idx - 1) 会从最后一个元素向前查找,这样就重复查找,且死循环了,所以要做一个判断,而且已经查找到第一个元素了,就该结束了)。

    说说lastIndexOf的用法。

    概述

    lastIndexOf() 方法返回指定元素(也即有效的 JavaScript 值或变量)在数组中的最后一个的索引,如果不存在则返回 -1。从数组的后面向前查找,从 fromIndex 处开始。

    语法

    arr.lastIndexOf(searchElement[, fromIndex = arr.length - 1])

    参数

    searchElement
    被查找的元素。
    fromIndex
    从此位置开始逆向查找。默认为数组的长度减 1,即整个数组都被查找。如果该值大于或等于数组的长度,则整个数组会被查找。如果为负值,将其视为从数组末尾向前的偏移。即使该值为负,数组仍然会被从后向前查找。如果该值为负时,其绝对值大于数组长度,则方法返回 -1,即数组不会被查找。

    描述

    lastIndexOf 使用严格相等(strict equality,即 ===)比较 searchElement 和数组中的元素。

    示例

    例子:使用 lastIndexOf

    下例使用 lastIndexOf 定位数组中的值。

     1 var array = [2, 5, 9, 2];
     2 var index = array.lastIndexOf(2);
     3 // index is 3
     4 index = array.lastIndexOf(7);
     5 // index is -1
     6 index = array.lastIndexOf(2, 3);
     7 // index is 3
     8 index = array.lastIndexOf(2, 2);
     9 // index is 0
    10 index = array.lastIndexOf(2, -2);
    11 // index is 0
    12 index = array.lastIndexOf(2, -1);
    13 // index is 3

    兼容旧环境(Polyfill)

    lastIndexOf 在 ECMA-262 标准第 5 版被添加。因此它在不兼容该标准的浏览器中可能不被支持。你可以把下面代码添加到脚本中来使那些没有实现该方法的实现环境支持该方法。该算法是被 ECMA-262 第 5 版指定的。假定 ObjectTypeErrorNumberMath.floorMath.abs,以及 Math.min 拥有其初始值。

     1 if (!Array.prototype.lastIndexOf) {
     2   Array.prototype.lastIndexOf = function(searchElement /*, fromIndex*/) {
     3     'use strict';
     4 
     5     if (this === void 0 || this === null) {
     6       throw new TypeError();
     7     }
     8 
     9     var n, k,
    10         t = Object(this),
    11         len = t.length >>> 0;
    12     if (len === 0) {
    13       return -1;
    14     }
    15 
    16     n = len - 1;
    17     if (arguments.length > 1) {
    18       n = Number(arguments[1]);
    19       if (n != n) {
    20         n = 0;
    21       }
    22       else if (n != 0 && n != (1 / 0) && n != -(1 / 0)) {
    23         n = (n > 0 || -1) * Math.floor(Math.abs(n));
    24       }
    25     }
    26 
    27     for (k = n >= 0
    28           ? Math.min(n, len - 1)
    29           : len - Math.abs(n); k >= 0; k--) {
    30       if (k in t && t[k] === searchElement) {
    31         return k;
    32       }
    33     }
    34     return -1;
    35   };
    36 }

    另外,该实现是为了绝对兼容 Firefox 和 the SpiderMonkey JavaScript 引擎中的 lastIndexOf,包括了几种临界情况。如果你要在实际应用中使用该实现,可以忽略这些临界情况,从而简化 fromIndex 的计算。

  • 相关阅读:
    Solution: Win 10 和 Ubuntu 16.04 LTS双系统, Win 10 不能从grub启动
    在Ubuntu上如何往fcitx里添加输入法
    LaTeX 笔记---Q&A
    Hong Kong Regional Online Preliminary 2016 C. Classrooms
    Codeforces 711E ZS and The Birthday Paradox
    poj 2342 anniversary party
    poj 1088 滑雪
    poj 2479 maximum sum
    poj 2481 cows
    poj 2352 stars
  • 原文地址:https://www.cnblogs.com/clear93/p/7928187.html
Copyright © 2011-2022 走看看