欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js Array對(duì)象的擴(kuò)展函數(shù)代碼

 更新時(shí)間:2013年04月24日 17:44:42   作者:  
有時(shí)候我們需要對(duì)js的array對(duì)象擴(kuò)展一些功能,這里簡(jiǎn)單介紹下,方便需要的朋友

使用

復(fù)制代碼 代碼如下:

<script language=javascript>
var isNumeric = function(x) {
   // returns true if x is numeric and false if it is not.
   var RegExp = /^(-)?(\d*)(\.?)(\d*)$/;
   return String(x).match(RegExp);
}
var myArray = [1,'two',3,'four',5,'six',7,'eight',9,'ten'];
var oddArray=myArray.filter(isNumeric);  // outputs: 1,3,5,7,9
var oddArray=myArray.some(isNumeric);  // outputs: true
var oddArray=myArray.every(isNumeric);  // outputs: false
var printArray =function(x, idx){
   document.writeln('['+idx+'] = '+x);
}
myArray.forEach(printArray);// outputs: [0] = 1 [1] = two [2] = 3 [3] = four [4] = 5
myArray.remove(9);
document.writeln(myArray);

復(fù)制代碼 代碼如下:

if (!Array.prototype.every)
{
  Array.prototype.every = function(fun /*, thisp*/)
  {
    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 /*, thisp*/)
  {
    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 /*, thisp*/)
  {
    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 /*, thisp*/)
  {
    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 /*, thisp*/)
  {
    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;
}
//隨機(jī)改變數(shù)組的排序
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;
}
//去掉數(shù)組中重復(fù)的值var a = new Array("5","7","7"); a.unique();
Array.prototype.unique = function() {
 var data = this || [];
    var a = {}; //聲明一個(gè)對(duì)象,javascript的對(duì)象可以當(dāng)哈希表用
    for (var i = 0; i < data.length; i++) {
        a[data[i]] = true;  //設(shè)置標(biāo)記,把數(shù)組的值當(dāng)下標(biāo),這樣就可以去掉重復(fù)的值
    }
    data.length = 0;

    for (var i in a) { //遍歷對(duì)象,把已標(biāo)記的還原成數(shù)組
        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 /*, 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);
}
/**
* 根據(jù)數(shù)組的下標(biāo)來(lái)刪除元素
*/ 
Array.prototype.removeByIndex=function($n) {  
    if($n<0){ //如果n<0,則不進(jìn)行任何操作。 
      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];}

//分開(kāi)添加,關(guān)鍵字shallow copy,如果遇到數(shù)組,復(fù)制數(shù)組中的元素
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))); //這里沒(méi)有直接處理參數(shù),而是復(fù)制了一下
 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)));
}

相關(guān)文章

  • 微信小程序?qū)崿F(xiàn)商品數(shù)據(jù)聯(lián)動(dòng)效果

    微信小程序?qū)崿F(xiàn)商品數(shù)據(jù)聯(lián)動(dòng)效果

    這篇文章主要介紹了微信小程序?qū)崿F(xiàn)商品數(shù)據(jù)聯(lián)動(dòng)效果,代碼很簡(jiǎn)單,直接復(fù)制即可根據(jù)自己的需求去修改,對(duì)小程序商品數(shù)據(jù)聯(lián)動(dòng)實(shí)例代碼感興趣的朋友一起看看吧
    2022-08-08
  • javascript的列表切換【實(shí)現(xiàn)代碼】

    javascript的列表切換【實(shí)現(xiàn)代碼】

    下面小編就為大家?guī)?lái)一篇javascript的列表切換【實(shí)現(xiàn)代碼】。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。
    2016-05-05
  • Bootstrap表單控件使用方法詳解

    Bootstrap表單控件使用方法詳解

    Bootstrap讓W(xué)eb開(kāi)發(fā)更迅速、更簡(jiǎn)單,這篇文章主要為大家詳細(xì)介紹了Bootstrap表單控件,用來(lái)與用戶做交流的一個(gè)網(wǎng)頁(yè)控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • js實(shí)現(xiàn)表單檢測(cè)及表單提示的方法

    js實(shí)現(xiàn)表單檢測(cè)及表單提示的方法

    這篇文章主要介紹了js實(shí)現(xiàn)表單檢測(cè)及表單提示的方法,涉及javascript表單元素提示效果的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-08-08
  • JS window對(duì)象的top、parent、opener含義介紹

    JS window對(duì)象的top、parent、opener含義介紹

    本文為大家介紹下JS window對(duì)象的top、parent、opener含義,不了解的朋友可以參考下,希望對(duì)大家有所幫助
    2013-12-12
  • 如何利用Three.js實(shí)現(xiàn)跳一跳小游戲

    如何利用Three.js實(shí)現(xiàn)跳一跳小游戲

    最近在公司寫H5的3D游戲,選擇了ThreeJS去做,做的過(guò)程中遇到了很多問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于如何利用Three.js實(shí)現(xiàn)跳一跳小游戲的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • layui導(dǎo)出所有數(shù)據(jù)的例子

    layui導(dǎo)出所有數(shù)據(jù)的例子

    今天小編就為大家分享一篇layui導(dǎo)出所有數(shù)據(jù)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-09-09
  • javascript類型系統(tǒng)_正則表達(dá)式RegExp類型詳解

    javascript類型系統(tǒng)_正則表達(dá)式RegExp類型詳解

    下面小編就為大家?guī)?lái)一篇javascript類型系統(tǒng)_正則表達(dá)式RegExp類型詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-06-06
  • 深入了解JavaScript中l(wèi)et/var/function的變量提升

    深入了解JavaScript中l(wèi)et/var/function的變量提升

    這篇文章主要介紹了深入了解JavaScript中l(wèi)et/var/function的變量提升,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-07-07
  • JS判斷微信掃碼的方法

    JS判斷微信掃碼的方法

    這篇文章通過(guò)代碼給大家介紹了JS判斷是否是微信掃碼的方法,非常不錯(cuò),需要的朋友參考下吧
    2017-08-08

最新評(píng)論