Array没有indexOf方法,这样在一个数组中查找某个元素的索引时比较麻烦,为了调用方便,于是通过prototype原型扩展了Array.prototype.indexOf():
Array.prototype.indexOf = function(item) {
for (var i = 0; i < this.length; i++) {
if (this[i] == item)
return i;
}
return -1;
}
但是由于Array在javascript1.6版本已经支持Array.indexOf(),所以高级浏览器ff、chrome等认为是自己原生支持的固有的属性。而IE8以下认为indexOf是“用户定义的属性”,这就出现了问题。 所以for in遍历数组元素的时候,要利用到hasOwnProperty.详见:
http://wangjingyi.iteye.com/blog/2038264
indexOf方法使用
全等(===)来判断一个元素是否符合搜索。搜索字符串及数字可能没有问题,但是搜索对象和数组可能会有问题。
// 可以判断两个对象的属性和值是否相等,但是不等判断两个对象是否相等,除非它们指向相同的地址
var e1 = {
"name": "wjy",
"blog": "http://www.wangjingyi.iteye.com"
},
e2 = {
"name": "zyc",
"blog": "http://www.zhangyaochun.iteye.com"
},
arr = [e1, e2],
index = arr.indexOf(e1);
console.log(index);//Outputs: 0
Array.prototype.indexOf()方法是在
ES5规范中添加的,在
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;
};
参考:
http://www.kwstu.com/ArticleView/manong_201411071635191494
http://www.jb51.net/article/35963.htm
分享到:
相关推荐
array.prototype.indexof 符合ES2015规范的shim / polyfill / replacement的Array.prototype.indexof ,可工作到ES3。 该软件包实现了接口。 它可以在ES3支持的环境中工作并符合。 因为Array.prototype.indexOf...
Array没有indexOf方法,这样在一个数组中查找某个元素的索引时比较麻烦,为了调用方便,于是通过prototype原型扩展了Array.prototype.indexOf(),这样用起来就比较方便了。但是这个自定义的indexOf在对数组进行遍历...
The program next reads in an index k from the user, then calls a method readValue(int [ ] a, int k) that would return the value of a[k]. The main program displays the value a[k]. If the index is out ...
背景:JavaScript中Array对象的标准方法中,没有indexOf()方法,可通过下面的代码扩展。 代码如下: if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt) { var len = this.length >>> 0; var ...
* @param {Object} o The object to check for * @param {Number} from (Optional) The index at which to begin the search * @return {Number} The index of o in the array (or -1 if it is not found) */
array.indexOf(‘REG') // 0 array.indexOf(‘R') // -1 array.indexOf('2018′) // 1 array.indexOf(2018) // -1 arr.indexOf(‘orange’) 输出 0 因为 ‘orange’ 是数组的第 0 个元素,匹配到并返回下标。 ...
Array.prototype.indexOf的更强大版本 安装 $ npm install array-index-of 例子 var indexOf = require ( 'array-index-of' ) ; var index = indexOf ( [ 1 , 2 , 3 ] , 2 ) ; // 1 var index2 = indexOf ( [ { a ...
Array没有indexOf方法,这样在一个数组中查找某个元素的索引时比较麻烦,为了调用方便,于是通过prototype原型扩展了Array.prototype.indexOf(),这样用起来就比较方便了。但是这个自定义的indexOf在对数组进行遍历...
Array.prototype.indexOf()的自定义实现 开始之前请阅读指南 阅读有关更多信息
ECMAScript 5 Array.prototype.indexOf 的 Shim。 基于。 安装 通过: component install gamtiq/array-indexof-shim 用法 var indexOf = require ( "array-indexof-shim" ) ; ... var nIndex1 = someArray . ...
Flex中 Array 的IndexOf用于在索引中从小到大查找,如果查得到就返回索引值,查不到就返回-1,下面有个示例,大家可以参考下
2.4.3 Directivity of Arrays of Resonant Elements 40 2.4.4 Planar Array Directivity 42 References 46 3 Linear Array Pattern Synthesis 49 3.1 Introduction 49 3.1.1 Pattern Formulations 49 3.1.2 Physics ...
详解PHP处理字符串类似indexof的方法函数 在PHP中处理字符串类似 indexof 的函数或方法有两个,它们是 strpos 函数和 stripos 函数,这两个函数的用法类似。 strpos 函数处理字符串时如果包含该字符串,则返回该...
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....
用jquery里的$.inArray替代indexOf $.inArray(“a”, divarr)==-1 以上这篇对象不支持indexOf属性或方法的解决方法(必看)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。
在JavaScript中,数组可以使用Array构造函数来创建,或使用[]快速创建,这也是首选的方法。数组是继承自Object的原型,并且他对typeof没有...比如IE8以下的浏览器不支持Array的indexOf方法,为了让数组支持indexOf,我