对应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. [html][javascript]动态增删页面元素

    <script type="text/javascript"> function append(event){ var myhref = document.create ...

  2. 视频描述(Video Captioning)调研

    Video Analysis 相关领域介绍之Video Captioning(视频to文字描述)http://blog.csdn.net/wzmsltw/article/details/7119238 ...

  3. 蚂蚁社招Java-第四轮电话面试【技术终面】

    作者:听着歌过面试链接:https://www.nowcoder.com/discuss/64708来源:牛客网 蚂蚁社招Java-第四轮电话面试[技术终面] 转载   (耗时22分钟,其实聊得东西挺 ...

  4. 让IE依据HTML头标签选择显示模式

    文件兼容性用于定义让IE如何编译你的网页.此文件解释文件兼容性,如何指定你网站的文件兼容性模式以及如何判断一个网页该使用的文件模式. 前言 为了帮助确保你的网页在所有未来的IE版本都有一致的外观,IE ...

  5. JS表单常见表达式(正则)

    整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$ 只能输入数字:"^[0-9]*$". 只能输入n位的数字:"^\d{n}$". 只能输入至少n ...

  6. OpenCL 前缀和

    ▶ 向量前缀和 ● 最简单的版本,代码 #include <stdio.h> #include <stdlib.h> #include <cl.h> const c ...

  7. selenium webdriver——JS滚动到指定位置

    1.DOM滚动方法 1.scrollIntoView(alignWithTop)  滚动浏览器窗口或容器元素,以便在当前视窗的可见范围看见当前元素.如果alignWithTop为true,或者省略它, ...

  8. java递归之“二叉树”

    物有本末,事有始终,知所先后,则近道矣.-----题记. BotWong半路入行it做码农,也就半年时间,竟“不知天高地厚”地来到了深圳闯天下.一口气投了百个简历,一周后终于有公司邀约面试,除了基础的 ...

  9. VBS 创建快捷方式

    Dim Shell,DesktopPath,link Set Shell = CreateObject("WScript.Shell") DesktopPath = Shell.S ...

  10. securecrt中使用上传下载sftp

    securecrt中使用上传下载sftp   SecureCRT这个工具自带了一个FTP,方便我们上传和下载,而且做的比较人性化,由于其基本命令和linux中基本命令大都相似,熟悉LINUX人能很容易 ...