使用Array.prototype.indexOf()的几点注意
对应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()的几点注意的更多相关文章
- [基础] Array.prototype.indexOf()查询方式
背景 最近在看Redux源码,createStore用于注册一个全局store,其内部维护一个Listeren数组,存放state变化时所有的响应函数. 其中store.subscribe(liste ...
- Array.prototype.indexOf
arr.indexOf(searchElement[, fromIndex = 0]) Array.prototype.indexOf()
- 有了 indexOf,为什么 ECMAScript 7 还添加了 Array.prototype.include
ECMAScript 7 中新增了用于检测数组中是否包含某个元素 Array.prototype.includes() API,想到了 Array 其实有很多相关 API 可以检测到是否包含某个元素, ...
- 终于解决了IE8不支持数组的indexOf方法,array的IndexOf方法
/* 终于解决了IE8不支持数组的indexOf方法 */ if (!Array.prototype.indexOf) { Array.prototype.indexOf = function (el ...
- JS Array常用方法indexOf/filter/forEach/map/reduce详解
Array共有九个方法 Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.protot ...
- 为Array 添加indexOf
为array赋予属性 if (!Array.prototype.indexOf) { Array.prototype.indexOf = function (elt /*, from*/) { var ...
- 5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
ES5中,一共有9个Array方法 Array.prototype.indexOf Array.prototype.lastIndexOf Array.prototype.every Array.pr ...
- 数组方法 Array.prototype
Object.prototype 数组的值是有序的集合,每一个值叫做元素,每一个元素在数组中都有数字位置编号,也就是索引,js中数组是弱类型的,数组中可以含有不同类型的元素.数组元素甚至可以是对象或者 ...
- [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 ...
随机推荐
- 安装特定版本 sysbench 以及安装时指定特定的mysql客户端
step1: 下载安装源码 https://github.com/akopytov/sysbench (git clone https://github.com/akopytov/sysbench.g ...
- http中的Content-Type
要学习content-type,必须事先知道它到底是什么,是干什么用的. HTTP协议(RFC2616)采用了请求/响应模型.客户端向服务器发送一个请求,请求头包含请求的方法.URI.协议版本.以及包 ...
- 2个版本并存的python使用新的版本安装django的方法
2个版本并存的python使用新的版本安装django的方法 默认是使用 pip install django 最新版的django会提示 要求python版本3.4以上,系统默认的版本是2.7.5 ...
- GridEh排序
添加pas文件 EhLibADO.pas EhLibCDS.pas EhLibFireDAC.pas 支持FireDAC 设置GridEh的属性 DBGridEh1->SortLocal = t ...
- CSS文档流、块级元素、内联元素
CSS文档流与块级元素(block).内联元素(inline),之前翻阅不少书籍,看过不少文章, 看到所多的是零碎的CSS布局基本知识,比较表面.看过O'Reilly的<CSS权威指南>, ...
- ASP.NET中高级程序员 面试题
1. 简要说一下.Net的编译过程. 2.ASP.NET与ASP的区别 3.谈一下ASP.NET页面生命周期 4.ASP.NET程序的运行机制.可以从一个页面的请求到返回的角度谈 5.Javascri ...
- Apache ZooKeeper 单机、集群部署文档
简介: Apache ZooKeeper 是一个分布式应用的高性能协调服务,功能包括:配置维护.统一命名.状态同步.集群管理.仲裁选举等. 下载地址:http://apache.fayea.com/z ...
- Wrapper模式(Decorator模式)
[Wrapper模式(Decorator模式)] 装饰者模式 Decorator模式(别名Wrapper):动态将职责附加到对象上,若要扩展功能,装饰者提供了比继承更具弹性的代替方案. 意图: 动态地 ...
- Prism之初识
首先,简单地介绍说一下单一应用程序与复合应用程序. 一.单一应用程序 看看上面这张图片,假如我们当前的需求是实现主界面如图所示.如果将其构建成具有用户控件的传统 WPF 应用程序,首先应构建一个顶层窗 ...
- SQL Server判断数据库、表、存储过程、函数是否存在
--判断数据库是否存在 if exists (select * from sys.databases where name = '数据库名') drop database [数据库名] --判断表是否 ...