手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >javascript数组的扩展实现代码集合
javascript数组的扩展实现代码集合
摘要:Array.prototype.del=function(n){if(n0){nr[nr.length]=me[t=Math.floor(M...

Array.prototype.del=function(n)

{

if(n<0)returnthis;

returnthis.slice(0,n).concat(this.slice(n+1,this.length));

}

//数组洗牌

Array.prototype.random=function()

{

varnr=[],me=this,t;

while(me.length>0)

{

nr[nr.length]=me[t=Math.floor(Math.random()*me.length)];

me=me.del(t);

}

returnnr;

}

//数字数组排序

Array.prototype.sortNum=function(f)

{

if(!f)f=0;

if(f==1)returnthis.sort(function(a,b){returnb-a;});

returnthis.sort(function(a,b){returna-b;});

}

//获得数字数组的最大项

Array.prototype.getMax=function()

{

returnthis.sortNum(1)[0];

}

//获得数字数组的最小项

Array.prototype.getMin=function()

{

returnthis.sortNum(0)[0];

}

//数组第一次出现指定元素值的位置

Array.prototype.indexOf=function(o)

{

for(vari=0;i<this.length;i++)if(this[i]==o)returni;

return-1;

}

//移除数组中重复的项

Array.prototype.removeRepeat=function()

{

this.sort();

varrs=[];

varcr=false;

for(vari=0;i<this.length;i++)

{

if(!cr)cr=this[i];

elseif(cr==this[i])rs[rs.length]=i;

elsecr=this[i];

}

varre=this;

for(vari=rs.length-1;i>=0;i--)re=re.del(rs[i]);

returnre;

}

例子:

vararr=["ni","wo","ta"];

删除数组中的“wo”

varnewArr=arr.del(1);

返回数组中“me”第一次出现的位置,若没有就返回-1

varstrPos=arr.indexOf("me");

【javascript数组的扩展实现代码集合】相关文章:

javascript实现日期按月份加减

用javascript动态注释掉HTML代码

JavaScript数据结构与算法之栈与队列

javascript动态创建链接的方法

javaScript中push函数用法实例分析

javascript检测两个数组是否相似

javascript相关事件的几个概念

javascript带回调函数的异步脚本载入方法实例分析

基于javascript简单实现对身份证校验

纯javascript实现四方向文本无缝滚动效果

精品推荐
分类导航