使用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;
};
时间: 2024-08-03 21:14:35

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

Array.prototype.indexOf

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title></head><body> <script> //参数item:必选项,要查找的Array对象中的一子项 //参数i:可选项.该整数值指出在Array对象内开始查找的索引.如果省略,则从字符串的

[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 Array.prototype.indexOf method, but now we have a simpler way: We can use the Array.prototype.includes method, which is available starting with ES2016.

数组方法 Array.prototype

Object.prototype 数组的值是有序的集合,每一个值叫做元素,每一个元素在数组中都有数字位置编号,也就是索引,js中数组是弱类型的,数组中可以含有不同类型的元素.数组元素甚至可以是对象或者其他数组 长度范围:1====2的23方-1 new Array(100)//undifind*100 arr[5]=10; arr.length//6 push() unshift() shift() pop() var Arr=[1,true,undifind,{x:1},[1,2,3]]; A

为Array 添加indexOf

为array赋予属性 if (!Array.prototype.indexOf) { Array.prototype.indexOf = function (elt /*, from*/) { var len = this.length >>> 0; var from = Number(arguments[1]) || 0; from = (from < 0) ? Math.ceil(from) : Math.floor(from); if (from < 0) from +

Array.prototype.slice &amp;&amp; Array.prototype.splice 用法阐述

目的 对于这两个数组操作接口,由于不理解, 往往被误用, 或者不知道如何使用.本文尝试给出容易理解的阐述. 数组 什么是数组? 数组是一个基本的数据结构, 是一个在内存中依照线性方式组织元素的方式, 其中元素的类型必须是相同的, 这个每个元素的索引地址才能被计算出来, 索引通常是数字,用来计算元素之间存储位置的偏移量. 结构如下: javascript数组 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glob

Array.prototype.removeBeginWithVal(删除数组内以某值开头的字符串对象)

Array扩展方法: 1 //author: Kenmu 2 //created time: 2015-03-16 3 //function: 删除数组内以某值开头的字符串对象 4 Array.prototype.removeBeginWithVal = function (val) { 5 for(var i=0, len = this.length; i < len; i++) { 6 if(this[i].indexOf(val) != -1) { 7 this.splice(i, 1);

Array 和 Array.prototype

定义 Array 对象是用于构造数组的全局对象,数组是类似于列表的高阶对象. Array.prototype 属性表示Array构造函数的原型,并允许您向所有Array对象添加新的属性和方法. 获取相应的属性名称 Object.getOwnPropertyNames(Array) //[ "length", "name", "prototype", "isArray", "from", "of&q

Array.prototype.reduce

[Array.prototype.reduce] Array.reduce([callback, initialValue]) 参数 callback 执行数组中每个值的函数,包含四个参数: previousValue 上一次调用回调函数返回的值,或者是提供的初始值(initialValue) currentValue 数组中当前被处理的元素 currentIndex 当前被处理元素在数组中的索引, 即currentValue的索引.如果有initialValue初始值, 从0开始.如果没有从1

01 - Execise About Array.prototype.reduce()

Description: Write a generic function chainer Write a generic function chainer that takes a starting value, and an array of functions to execute on it (array of symbols for ruby). The input for each function is the output of the previous function (ex