欢迎您访问我爱IT技术网,今天小编为你分享的javascript教程:【js Array对象的扩展函数代码】,下面是详细的讲解!
js Array对象的扩展函数代码
if (!Array.prototype.every)
{
Array.prototype.every=function(fun )
{
var len=this.length;
if (typeof fun !="function")
throw new TypeError();
var thisp=arguments[1];
for (var i=0; i < len; i++)
{
if (i in this &&
!fun.call(thisp, this[i], i, this))
return false;
}
return true;
};
}
if (!Array.prototype.filter)
{
Array.prototype.filter=function(fun )
{
var len=this.length;
if (typeof fun !="function")
throw new TypeError();
var res=new Array();
var thisp=arguments[1];
for (var i=0; i < len; i++)
{
if (i in this)
{
var val=this[i]; // in case fun mutates this
if (fun.call(thisp, val, i, this))
res.push(val);
}
}
return res;
};
}
if (!Array.prototype.forEach)
{
Array.prototype.forEach=function(fun )
{
var len=this.length;
if (typeof fun !="function")
throw new TypeError();
var thisp=arguments[1];
for (var i=0; i < len; i++)
{
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
}
if (!Array.prototype.map)
{
Array.prototype.map=function(fun )
{
var len=this.length;
if (typeof fun !="function")
throw new TypeError();
var res=new Array(len);
var thisp=arguments[1];
for (var i=0; i < len; i++)
{
if (i in this)
res[i]=fun.call(thisp, this[i], i, this);
}
return res;
};
}
if (!Array.prototype.some)
{
Array.prototype.some=function(fun )
{
var len=this.length;
if (typeof fun !="function")
throw new TypeError();
var thisp=arguments[1];
for (var i=0; i < len; i++)
{
if (i in this &&
fun.call(thisp, this[i], i, this))
return true;
}
return false;
};
}
Array.prototype.sortNum=function() {
return this.sort( function (a,b) { return a-b; } );
}
<!--
var tmp=[5,9,12,18,56,1,10,42,'blue',30, 7,97,53,33,30,35,27,30,'35','Ball', 'bubble'];
var thirty=tmp.find(30); // Returns 9, 14, 17
var thirtyfive=tmp.find('35'); // Returns 18
var thirtyfive=tmp.find(35); // Returns 15
var haveBlue=tmp.find('blue'); // Returns 8
var notFound=tmp.find('not there!'); // Returns false
var regexp1=tmp.find(/^b/); // returns 8,20 (first letter starts with b)
var regexp1=tmp.find(/^b/i); // returns 8,19,20 (same as above but ignore case)
-->
Array.prototype.find=function(searchStr) {
var returnArray=false;
for (i=0; i<this.length; i++) {
if (typeof(searchStr)=='function') {
if (searchStr.test(this[i])) {
if (!returnArray) { returnArray=[] }
returnArray.push(i);
}
} else {
if (this[i]===searchStr) {
if (!returnArray) { returnArray=[] }
returnArray.push(i);
}
}
}
return returnArray;
}
//随机改变数组的排序
Array.prototype.shuffle=function (){
for(var rnd, tmp, i=this.length; i; rnd=parseInt(Math.random()*i), tmp=this[--i], this[i]=this[rnd], this[rnd]=tmp);
return this;
}
<!--var myArray=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var yourArray=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
document.writeln(myArray.compare(yourArray)); // outputs: true;-->
Array.prototype.compare=function(testArr) {
if (this.length !=testArr.length) return false;
for (var i=0; i < testArr.length; i++) {
if (this[i].compare) {
if (!this[i].compare(testArr[i])) return false;
}
if (this[i] !==testArr[i]) return false;
}
return true;
}
//去掉数组中重复的值var a=new Array("5","7","7"); a.unique();
Array.prototype.unique=function() {
var data=this || [];
var a={}; //声明一个对象,javascript的对象可以当哈希表用
for (var i=0; i < data.length; i++) {
a[data[i]]=true; //设置标记,把数组的值当下标,这样就可以去掉重复的值
}
data.length=0;
for (var i in a) { //遍历对象,把已标记的还原成数组
this[data.length]=i;
}
return data;
}
Array.prototype.addAll=function($array)
{
if($array==null || $array.length==0)
return;
for(var $i=0; $i<$array.length; $i++)
this.push($array[$i]);
}
Array.prototype.contains=function($value)
{
for(var $i=0; $i<this.length; $i++)
{
var $element=this[$i];
if($element==$value)
return true;
}
return false;
}
Array.prototype.indexOf=function($value)
{
for(var $i=0; $i<this.length; $i++)
{
if(this[$i]==$value)
return $i;
}
return -1;
}
if (!Array.prototype.lastIndexOf)
{
Array.prototype.lastIndexOf=function(elt )
{
var len=this.length;
var from=Number(arguments[1]);
if (isNaN(from))
{
from=len - 1;
}
else
{
from=(from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from +=len;
else if (from >=len)
from=len - 1;
}
for (; from > -1; from--)
{
if (from in this &&
this[from]===elt)
return from;
}
return -1;
};
}
Array.prototype.insertAt=function($value, $index)
{
if($index < 0)
this.unshift($value);
else if($index >=this.length)
this.push($value);
else
this.splice($index, 0, $value);
}
Array.prototype.removeByIndex=function($n) {
if($n<0){ //如果n<0,则不进行任何操作。
return this;
}else{
return this.slice(0,$n).concat(this.slice($n+1,this.length));
}
}
//依赖indexOf
Array.prototype.remove=function($value)
{
var $index=this.indexOf($value);
if($index !=-1)
this.splice($index, 1);
}
Array.prototype.removeAll=function()
{
while(this.length > 0)
this.pop();
}
Array.prototype.replace=function($oldValue, $newValue)
{
for(var $i=0; $i<this.length; $i++)
{
if(this[$i]==$oldValue)
{
this[$i]=$newValue;
return;
}
}
}
Array.prototype.swap=function($a, $b)
{
if($a==$b)
return;
var $tmp=this[$a];
this[$a]=this[$b];
this[$b]=$tmp;
}
Array.prototype.max=function() {
return Math.max.apply({}, this);
}
Array.prototype.min=function() {
return Math.min.apply({}, this);
}
Array.prototype.splice=function(start, delLen, item){
var len=this.length;
start=start<0?0:start>len?len:start?start:0;
delLen=delLen<0?0:delLen>len?len:delLen?delLen:len;
var arr=[],res=[];
var iarr=0,ires=0,i=0;
for(i=0;i<len;i++){
if(i<start|| ires>=delLen) arr[iarr++]=this[i];
else {
res[ires++]=this[i];
if(item&&ires==delLen){
arr[iarr++]=item;
}
}
}
if(item&&ires<delLen) arr[iarr]=item;
for(var i=0;i<arr.length;i++){
this[i]=arr[i];
}
this.length=arr.length;
return res;
}
Array.prototype.shift=function(){ if(!this) return[];return this.splice(0,1)[0];}
//分开添加,关键字shallow copy,如果遇到数组,复制数组中的元素
Array.prototype.concat=function(){
var i=0;
while(i<arguments.length){
if(typeof arguments[i]==='object'&&typeof arguments[i].splice==='function' &&!arguments[i].propertyIsEnumerable('length')){
// NOT SHALLOW COPY BELOW
// Array.prototype.concat.apply(this,arguments[i++]);
var j=0;
while(j<arguments[i].length) this.splice(this.length,0,arguments[i][j++]);
i++;
} else{
this[this.length]=arguments[i++];
}
}
return this;
}
Array.prototype.join=function(separator){
var i=0,str="";
while(i<this.length) str+=this[i++]+separator;
return str;
}
Array.prototype.pop=function() { return this.splice(this.length-1,1)[0];}
Array.prototype.push=function(){
Array.prototype.splice.apply(this,
[this.length,0].concat(Array.prototype.slice.apply(arguments))); //这里没有直接处理参数,而是复制了一下
return this.length;
}
Array.prototype.reverse=function(){
for(var i=0;i<this.length/2;i++){
var temp=this[i];
this[i]=this[this.length-1-i];
this[this.length-1-i]=temp;
}
return this;
}
Array.prototype.slice=function(start, end){
var len=this.length;
start=start<0?start+=len:start?start:0;
end=end<0?end+=len:end>len?len:end?end:len;
var i=start;
var res=[];
while(i<end){
res.push(this[i++]);
}
return res;
}
//arr.unshift(ele1,ele2,ele3....)
Array.prototype.unshift=function(){
Array.prototype.splice.apply(this,[0,0].concat(Array.prototype.slice.apply(this,arguments)));
}
关于js Array对象的扩展函数代码的用户互动如下:
相关问题:js中函数里的参数对象arguments是个数组吗?
答:Javascript函数中的参数对象arguments是个对象,而不是数组。但它可以类似数组那样通过数字下表访问其中的元素,而且它也有length属性标识它的元素的个数。通常我们把它转换成数组用Array的slice函数,示例代码如下:function fn() { var arr = ... >>详细
相关问题:关于javascript问题 Array数组对象扩展的问题
答:人家那些是浏览器解析JS的时候自动加上去的(也就是解析器中默认的),而你这个是你自己写进去的,当然不能作为一个属性了,所以它被写入了数组,但是你可以看到,在length的时候并没有算它,说明它也是数组的一个属性,只是没有写到下面。所以... >>详细
相关问题:js中如何用{}来定义函数??
答:代码: a={ val:9, fc:function(){ alert('1'); }}调用a.fc();原理: js是弱语言,相对来说语法比较宽松,主要掌握常用的三个对象,函数(function),数组(array或[]定义),对象({}方式定义)。这三者是可以相互组合的,上面也就是一个组合。 比如:... >>详细
- 【firefox】firefox浏览器不支持innerText的解决
- 【Extjs】Extjs学习过程中新手容易碰到的低级错误
- 【clearInterval】js clearInterval()方法的定义
- 【ComboBox】ComboBox 和 DateField 在IE下消失的
- 【dom】javascript dom追加内容实现示例-追加内容
- 【checkbox】让checkbox不选中即将选中的checkbox
- 【Array】js中更短的 Array 类型转换-类型转换
- 【append】append和appendTo的区别以及appendChil
- 【ExtJs】ExtJs 表单提交登陆实现代码-表单提交-
- 【ajax清除浏览器缓存】Ajax清除浏览器js、css、
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-