var arr = ["Hello", "World", "Hello", "World"]; var pos1 = arr.indexOf("Hello"); var pos2 = arr.lastIndexOf("Hello"); console.log(pos1); \\ 0 console.log(pos2); \\ 3
兼容IE6\7\8javascript
if(!Array.prototype.indexOf) { Array.prototype.indexOf = function(item) { var arr = this; for(var i = 0; i < arr.length; i ++) { if(arr[i] === item) { return i; } } return -1; } Array.prototype.lastIndexOf = function(item) { var arr = this; for(var i = arr.length - 1; i > -1; i --) { if(arr[i] === item) { return i; } } return -1; } }
兼容IE6\7\8java
if(!Array.prototype.forEach) { Array.prototype.forEach = function(fn) { for(var i = 0; i < this.length; i ++) { fn(this[i], i, this); } } }
$.each(arr, function(index, value));
兼容IE6\7\8數組
if(!Arrray.prototype.map) { Array.prototype.map = function(fn) { var result = new Array(); for(var i = 0; i < this.length; i ++) { result.push(fn(this[i], i, this)); } return result; } }
兼容IE6\7\8函數
if(!Array.prototype.some) { Array.prototype.some = function(fn) { for(var i = 0; i < this.length; i ++) { if(fn()) { return true; } } return false; } }
兼容IE6\7\8this
if(!Array.prototype.every) { Array.prototype.every = function(fn) { for(var i = 0; i < this.length; i ++) } }