当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript数组的扩展实现代码集合

Javascript
Add a Table to a Word Document
Add Formatted Text to a Word Document
用jscript实现新建word文档
用jscript实现新建和保存一个word文档
Open and Print a Word Document
Use Word to Search for Files
Convert Seconds To Hours
Sample script that deletes a SQL Server database
Sample script that displays all of the users in a given SQL Server DB
firefox中用javascript实现鼠标位置的定位
div+css实现鼠标放上去,背景跟图片都会变化。
Locate a File Using a File Open Dialog Box
Save a File Using a File Save Dialog Box
用jscript实现列出安装的软件列表
List the Stored Procedures in a SQL Server database
Display SQL Server Login Mode
Display SQL Server Version Information
List all the Databases on a SQL Server
用jscript启动sqlserver
Stop SQL Server

Javascript 中的 javascript数组的扩展实现代码集合


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 241 ::
收藏到网摘: n/a

非常不错的javascript数据功能增强函数 Array.prototype.del = function(n)
{
if (n<0) return this;
return this.slice(0,n).concat(this.slice(n+1,this.length));
}
// 数组洗牌
Array.prototype.random = function()
{
var nr=[], me=this, t;
while(me.length>0)
{
nr[nr.length] = me[t = Math.floor(Math.random() * me.length)];
me = me.del(t);
}
return nr;
}
// 数字数组排序
Array.prototype.sortNum = function(f)
{
if (!f) f=0;
if (f==1) return this.sort(function(a,b){return b-a;});
return this.sort(function(a,b){return a-b;});
}
// 获得数字数组的最大项
Array.prototype.getMax = function()
{
return this.sortNum(1)[0];
}
// 获得数字数组的最小项
Array.prototype.getMin = function()
{
return this.sortNum(0)[0];
}
// 数组第一次出现指定元素值的位置
Array.prototype.indexOf = function(o)
{
for (var i=0; i<this.length; i++) if (this[i]==o) return i;
return -1;
}
// 移除数组中重复的项
Array.prototype.removeRepeat=function()
{
this.sort();
var rs = [];
var cr = false;
for (var i=0; i<this.length; i++)
{
if (!cr) cr = this[i];
else if (cr==this[i]) rs[rs.length] = i;
else cr = this[i];
}
var re = this;
for (var i=rs.length-1; i>=0; i--) re = re.del(rs[i]);
return re;
}
例子:
var arr=["ni","wo","ta"];
删除数组中的“wo”
var newArr=arr.del(1);
返回数组中“me”第一次出现的位置,若没有就返回-1
var strPos=arr.indexOf("me");