// indexOf
Array.prototype.indexOf = function(e){ for(var i=0,j; j=this[i]; i++){ if(j==e){return i;} } return -1; }
// delete
Array.prototype.remove = function(from, to) { var rest = this.slice((to || from) + 1 || this.length); this.length = from < 0 ? this.length + from : from; return this.push.apply(this, rest); };
// unquie
Array.prototype.unquie=function(){
var newArray=new Array();
var len=this.length;
for (var i=0;i<len ;i++){
for(var j=i+1;j<len;j++){
if(this[i]===this[j]){
j=++i;
}
}
newArray.push(this[i]);
}
return newArray;
}
本文介绍并实现了三种实用的JavaScript数组原型方法:自定义indexOf方法用于查找元素索引;remove方法用于删除数组中指定范围的元素;以及unquie方法实现数组去重功能。这些方法能够帮助开发者更高效地处理数组数据。
2万+

被折叠的 条评论
为什么被折叠?



