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

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 214 ::
收藏到网摘: 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");