对应indexOf这个方法,在日常开发中比较常见的应该是String.prototype.indexOf()方法,Array.prototype.indexOf()方法和其有很大的相似性,本文不想去描述其的基本用法,而是去探究在使用中需要考虑的一些问题。

一、性能

在数组元素少的情况下,我们虽然只是跳过一个元素来检索,性能微不足道,但是当我们正在处理数以千计的元素,如果使用indexOf()的第二个参数,你可能获得性能上的显著提升。

二、全等(===)

indexOf方法使用全等(===)来判断一个元素是否符合您的搜索。搜索字符串及数字可能没有问题,但是搜索对象和数组可能会有问题,看下面一个实例:

var arr = [{
"name": "Benjamin",
"blog": "http://www.zuojj.com"
},{
"name": "John",
"blog": "http://www.john.com"
}],
index = arr.indexOf({
"name": "Benjamin",
"blog": "http://www.zuojj.com"
}); //Outputs: -1
console.log(index);

实例输出结果为-1,为什么?其实就是判断两个对象是否相等的问题,在本专题中,写过一篇文章Javascript 判断对象是否相等,大家可以看看。我们可以判断两个对象的属性和值是否相等,但是不等判断两个对象是否相等,除非它们指向相同的地址。 修改上例,可以得到我们期望的结果:

var e1 = {
"name": "Benjamin",
"blog": "http://www.zuojj.com"
},
e2 = {
"name": "John",
"blog": "http://www.john.com"
},
arr = [e1, e2],
index = arr.indexOf(e1); //Outputs: 0
console.log(index);

三、兼容性

Array.prototype.indexOf()方法是在ES5规范中添加的,同filter/every/some/reduce/map等方法一样,在IE8及以下浏览器不支持,可以使用下面的Polyfill或者一些封装库Underscore or Lo-Dash来兼容。

Array.prototype.indexOf = Array.prototype.indexOf || function (searchElement, fromIndex) {
if ( this === undefined || this === null ) {
throw new TypeError( '"this" is null or not defined' );
} var length = this.length >>> 0; // Hack to convert object.length to a UInt32 fromIndex = +fromIndex || 0; if (Math.abs(fromIndex) === Infinity) {
fromIndex = 0;
} if (fromIndex < 0) {
fromIndex += length; if (fromIndex < 0) {
fromIndex = 0;
}
} for (; fromIndex < length; fromIndex++) {
if (this[fromIndex] === searchElement) {
return fromIndex;
}
} return -1;
};

使用Array.prototype.indexOf()的几点注意的更多相关文章

  1. [基础] Array.prototype.indexOf()查询方式

    背景 最近在看Redux源码,createStore用于注册一个全局store,其内部维护一个Listeren数组,存放state变化时所有的响应函数. 其中store.subscribe(liste ...

  2. Array.prototype.indexOf

    arr.indexOf(searchElement[, fromIndex = 0]) Array.prototype.indexOf()

  3. 有了 indexOf,为什么 ECMAScript 7 还添加了 Array.prototype.include

    ECMAScript 7 中新增了用于检测数组中是否包含某个元素 Array.prototype.includes() API,想到了 Array 其实有很多相关 API 可以检测到是否包含某个元素, ...

  4. 终于解决了IE8不支持数组的indexOf方法,array的IndexOf方法

    /* 终于解决了IE8不支持数组的indexOf方法 */ if (!Array.prototype.indexOf) { Array.prototype.indexOf = function (el ...

  5. JS Array常用方法indexOf/filter/forEach/map/reduce详解

    Array共有九个方法   Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.protot ...

  6. 为Array 添加indexOf

    为array赋予属性 if (!Array.prototype.indexOf) { Array.prototype.indexOf = function (elt /*, from*/) { var ...

  7. 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例

    ES5中,一共有9个Array方法 Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.pr ...

  8. 数组方法 Array.prototype

    Object.prototype 数组的值是有序的集合,每一个值叫做元素,每一个元素在数组中都有数字位置编号,也就是索引,js中数组是弱类型的,数组中可以含有不同类型的元素.数组元素甚至可以是对象或者 ...

  9. [ES2016] Check if an array contains an item using Array.prototype.includes

    We often want to check if an array includes a specific item. It's been common to do this with the Ar ...

随机推荐

  1. CFGym 101158B(巨坑题)

    前言:无话可说,看懂题意就没什么难度了. 题意:对于[0, 9999]区间内的每一个数b,通过输入的转换表,得到一个e值,把这个值添加到b的后面,得到一个五位数c.对于c的每一位,从0枚举到9,得到5 ...

  2. 简单神经网络TensorFlow实现

    学习TensorFlow笔记 import tensorflow as tf #定义变量 #Variable 定义张量及shape w1= tf.Variable(tf.random_normal([ ...

  3. Androdi Gradle build project info 很慢

    Androdi Gradle build project info 很慢 http://blog.csdn.net/stupid56862/article/details/78345584   原创  ...

  4. 【CentOS 6.5】【转】新版本linux生成xorg.conf

    新版本的linux如何生成xorg.conf 较新版本的linux系统都已经没有xorg.conf文件,但是有时候为了对显示做微调或为了支持多屏显示等原因,还需要手工生成一个xorg.conf,然后根 ...

  5. mock 的独立使用

    public class Air21QueryMileStoneJobTest{ @InjectMocks Air21QueryMileStoneJob air21QueryMileStoneJob ...

  6. mybatis相对于ibatis的优势

    2010年,apache的Ibatis框架停止更新,并移交给了google团队,同时更名为MyBatis.从2010年后Ibatis在没更新过,彻底变成了一个孤儿框架.一个没人维护的框架注定被myba ...

  7. 学习 TCP 三次握手和四次挥手

    TCP三次握手和四次挥手的问题在面试中是最为常见的考点之一.很多读者都知道三次和四次,但是如果问深入一点,他们往往都无法作出准确回答. 本篇尝试使用动画来对这个知识点进行讲解,期望读者们可以更加简单地 ...

  8. http post Content-type: application/json; charset=utf-8

      The header just denotes what the content is encoded in. It is not necessarily possible to deduce t ...

  9. Vertex color blending & UV tiling

    [Vertex color blending & UV tiling] 1.GemotryData控件用于代码顶点数据,如网格中的Vertex Color(下左图),UV Coord(下右图) ...

  10. TensorMask

    原文地址:https://arxiv.org/pdf/1903.12174.pdf 论文阅读:http://www.zhenzhujue.cn/article-36456-1.html https:/ ...