javascript unshift()和shift()

來源:互聯網
上載者:User

標籤:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body><script type="text/javascript">console.log(‘unshift()和shift()方法的行為非常類似於push()和pop(),不一樣的是前者在數組的頭部而非尾部進行元素的插入和刪除操作。‘);console.log(‘unshift() 在數組的頭部添加一個或多個元素,並將已存在的元素移動到更高索引的位置來獲得足夠的空間,最後返回數組新的長度。‘);console.log(‘shift()刪除數組的第一個元素並將其返回,然後把所有隨後的元素下移一個位置來填補數組頭部的空缺。‘);var a=[];var ret=a.unshift(1);console.log(‘01. a.unshift(1)   a=‘+a+‘ : ret=‘+ret);ret =a.unshift(22);console.log(‘02. a.unshift(22)  a=‘+a+‘ : ret=‘+ret);a.shift();console.log(‘03. a.shift()  a=‘+a+‘ : ret=‘+ret);a.unshift(3,[4,5]);console.log(‘04. a.unshift(3,[4,5])  a=‘+a+‘ : ret=‘+ret);a.shift();console.log(‘05. a.shift()  a=‘+a+‘ : ret=‘+ret);a.shift();console.log(‘06. a.shift()  a=‘+a+‘ : ret=‘+ret);a.shift();console.log(‘07. a.shift()  a=‘+a+‘ : ret=‘+ret);</script></body></html>

javascript unshift()和shift()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.