js數組依據下標刪除元素,js數組依據下標

來源:互聯網
上載者:User

js數組依據下標刪除元素,js數組依據下標

1、建立數組

複製代碼 代碼如下:
var array = new Array();
var array = new Array(size);//指定數組的長度
var array = new Array(item1,item2……itemN);//建立數組並賦值

2、取值、賦值

複製代碼 代碼如下:
var item = array[index];//擷取指定元素的值
array[index] = value;//為指定元素賦值

3、添加新元素

複製代碼 代碼如下:
array.push(item1,item2……itemN);//將一個或多個元素加入數組,返回新數組的長度
array.unshift(item1,item2……itemN);//將一個或多個元素加入到數組的開始位置,原有元素位置自動後移,返回  新數組的長度
array.splice(start,delCount,item1,item2……itemN);//從start的位置開始向後刪除delCount個元素,然後從start的位置開始插入一個或多個新元素

4、刪除元素

複製代碼 代碼如下:
array.pop();//刪除最後一個元素,並返回該元素
array.shift();//刪除第一個元素,數組元素位置自動前移,返回被刪除的元素
array.splice(start,delCount);//從start的位置開始向後刪除delCount個元素

5、數組的合并、截取

複製代碼 代碼如下:
array.slice(start,end);
//以數組的形式返回數組的一部分,注意不包括 end 對應的元素,如果省略 end 將複製 start 之後的所有元素
array.concat(array1,array2);
//將多個數組拼接成一個數組

6、數組的排序

複製代碼 代碼如下:
array.reverse();//數組反轉
array.sort();//數組排序,返回數組地址

7、數組轉字串

array.join(separator);//將數組原因用separator串連起來

列了這麼都就是沒有發現刪除數組元素的方法!於是查了一些資料找到瞭解決方法。
刪除數組元素需要擴充Array原型prototype.

Array.prototype.del=function(index){    if(isNaN(index)||index>=this.length){      return false;    }    for(var i=0,n=0;i      if(this[i]!=this[index]){        this[n++]=this[i];      }    }    this.length-=1;  };

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.