对应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. 通过BeanFactoryPostProcessor来获取bean

    一.现在我们很多时候都在spring的容器中,进行bean的提取和操作,但是配置里面首先需要扫描相应的包来实现相关bean的注入 但是问题来了.如果我们在另外一个线程需要用的时候,并且没有配置扫描该类 ...

  2. 修改jvm xms参数

    http://hi.baidu.com/200770842223/item/9358aad4f3194e1a20e2501b http://www.cnblogs.com/mingforyou/arc ...

  3. 精《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #5 使用checkpatch.pl检查补丁的格式

    HACK #5 使用checkpatch.pl检查补丁的格式 本节介绍发布前检查补丁格式的方法.Linux内核是由多个开发者进行开发的.因此,为了保持补丁评估与源代码的可读性,按照统一的规则进行编写是 ...

  4. Hyberledger-Fabric 1.00 RPC学习(2)尝试建立一个network

    本文参考:http://hyperledger-fabric.readthedocs.io/en/latest/build_network.html 这里我们学习建立第一个Hyperledger Fa ...

  5. C# 中带有中国农历的日期选择控件

    开源一款自己刚开始接触 C# 时开发的带有农历信息的日期选择控件,记得那时还是在2010年的寒假期间做的这个东西.刚开始接触 C# 时,使用WinForm来开发桌面程序,觉得简直是简单又迅速,由于 C ...

  6. STL string 常用函数(转)

    string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...

  7. (网页的缓存控制)HTML配置no-cache(备忘) “Cache-control”常见的取值

    HTML配置no-cache(备忘) No-cache配置 html表头如下 <meta http-equiv="Content-Type" content="te ...

  8. Spring Boot下Druid连接池的使用配置分析

    https://blog.csdn.net/blueheart20/article/details/52384032

  9. Linux CentOS修改网卡IP/网关设置

    1. 修改对应网卡IP的配置文件 修改以下内容 2. 修改对应网卡的网关的配置文件 vi /etc/sysconfig/network 修改以下内容 3. CentOS 修改DNS 修改以下内容 4. ...

  10. SpinBlur - 旋转模糊

    [SpinBlur - 旋转模糊] Using the Spin Blur effect, you can rotate and blur the image around one or more p ...