学习电脑,计算机系统故障维护,电脑技术,电脑知识学习-就上第二电脑网
当前位置: 首页 > 网络编程 > JavaScript

原生js怎么删除classjs Array对象的扩展函数代码

 更新时间: 2019-07-06 15:43:07   作者:第二电脑网   来源:第二电脑网   浏览数:896   我要评论

使用 代码如下:<scipt laguage=javascipt>va isNumeic = fuctio(x) {   // etus tue if x is umeic ad false if it is ot.   va RegExp = /^(-)?(\d*

var yourArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
document.writeln(myArraypare(yourArray)); // outputs: true;-->
Array.prototypepare = function(testArr) {
    if (this.length != testArr.length) return false;
    for (var i = 0; i < testArr.length; i++) {
        if (this[i]pare) {
            if (!this[i]pare(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;
 }

www.002pc.com认为此文章对《js Array对象的扩展函数代码》说的很在理,002pc.com为你提供最佳的网站功能开发,帝国cms二次开发。

 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 /*, from*/)
  {
    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];}
更多:原生js怎么删除classjs Array对象的扩展函数代码
https://www.002pc.comhttps://www.002pc.com/javascript/995.html

你可能感兴趣的js,Array,代码,函数,扩展,对象

关于我们 - 广告合作 - 联系我们 - 免责声明 - 网站地图 - 投诉建议 - 在线投稿

  浙ICP备140365454号

©CopyRight 2008-2020 002pc.COM Inc All Rights Reserved. 第二电脑网 版权所有 联系QQ:282523118