最近写js也多了,Array中有好多方法不够用。自己加了些以后还可能用到。<script type="text/javascript"> //找到返回所在索引,不存在返回-1 Array.prototype.index = function (el) { var i = 0; for (var i = 0, len = this.length; i < len; i++) { if (el == this[i]) { return i; } } return -1; }; //是否相同,相同返回true,不同返回false Array.prototype.same = function () { if (this.length == 0) { return true; } var temp = this.join('').replaceAll(this[0], ""); if (temp != "") { return false; } else { return true } }; //replaceAll函数参考: //判断是否存在重复数据 Array.prototype.iterate = function () { var flag = false; for (var i = 0, len = this.length; i < len; i++) { for (var j = 0; j < len; j++) { if (this[i] == this[j] && i != j) { flag = true; break; } } } return flag; }; //传入一个回调,根据回调函数条件过滤数组 Array.prototype.filter = function(callback) { var rs = []; for ( var i = 0, length = this.length; i < length; i++ ) { if (callback(this[i], i)) { rs.push(this[i]); } } return rs; }; window.onload = function () { var ary = [1, 2, 3, 4, 6, 4, 5]; alert(ary.index(6)); alert(ary.same()); alert(ary.iterate()); var a = ary.filter(function (n, i) { return n > 4; }); alert(a.length + "-" + a[0]); } </script>