JavaScript Array 對象常用方法

來源:互聯網
上載者:User
<script type="text/javascript">    //shift:刪除原數組的第一項,返回刪除元素的值;如果數組為空白則返回undefined    var arr = [1, 2, 3, 4, 5];    var out = arr.shift();    console.log(arr); //[2,3,4,5]    console.log(out); //1    var arr = [];    var out = arr.shift();    console.log(arr); //[]    console.log(out); //undefined    //unshift:將參數添加到原數組開頭,返回數組的長度(ie6下返回undefined)    var arr = [1, 2];    var out = arr.unshift(-1, 0);    console.log(arr); //[-1,0,1,2]    console.log(out); //4    //pop:刪除原數組的最後一項,返回數組刪除的值;如果數組為空白則返回undefined    var arr = [1, 2, 3, 4, 5];    var out = arr.pop();    console.log(arr); //[1,2,3,4]    console.log(out); //5    var arr = [];    var out = arr.pop();    console.log(arr); //[]    console.log(out); //undefined    //push:將參數添加到原數組的末尾,返回數組的長度    var arr = [1, 2, 3];    var out = arr.push(4, 5, 6);    console.log(arr); //[1,2,3,4,5,6]    console.log(out); //6    //concat:返回一個將參數添加到原數組中構成的新數組    var arr = [1, 2, 3];    var out = arr.concat(4, 5);    console.log(arr); //[1,2,3]    console.log(out); //[1,2,3,4,5]    //splice(start,deleteCount,val1,val2,...):從start位置開始刪除原數組deleteCount項,並從該位置起插入val1,val2,...,返回刪除的項組成的新數組    var arr = [1, 2, 3, 4, 5];    var out = arr.splice(2, 2, 7, 8, 9, 10);    console.log(arr); //[1,2,7,8,9,10,5]    console.log(out); //[3,4]    //在清空數組時僅需傳遞start,如果不刪除所有元素,再傳遞deleteCount    var arr = [1, 2, 3, 4, 5];    var out = arr.splice(2);    console.log(arr); //[1,2]    console.log(out); //[3,4,5]    //同shift    var arr = [1, 2, 3, 4, 5];    var out = arr.splice(0, 1);    console.log(arr); //[2,3,4,5]    console.log(out); //[1]    //同unshift    var arr = [1, 2, 3, 4, 5];    var out = arr.splice(0, 0, 22, -1, 0);    console.log(arr); //[22,-1,0,1,2,3,4,5]    console.log(out); //[]    //同pop    var arr = [1, 2, 3, 4, 5];    var out = arr.splice(arr.length - 1, 1);    console.log(arr); //[1,2,3,4]    console.log(out); //[5]    //同push    var arr = [1, 2, 3, 4, 5];    var out = arr.splice(arr.length, 0, 6, 7, 8);    console.log(arr); //[1,2,3,4,5,6,7,8]    console.log(out); //[]    //reverse:將原數組倒序;    var arr = ['a', 1, {}, [2, 3]];    console.log(arr); //['a', 1, {}, [2, 3]]    var out = arr.reverse();    console.log(arr); //[[2,3],{},1,'a']    console.log(out); //[[2,3],{},1,'a']    var arr = [0, 1, 5, 10, 15];    arr.reverse();    console.log(arr); //[15,10,5,1,0]    var arr = [1, 0, 5, 15, 10];    arr.reverse();    console.log(arr); //[10,15,5,0,1]    //sort(orderfunction):sort()為升序排列,但是先調用每個數組項的toString()方法,然後比較字串來排序,是按ASCII進行比較的;    //所以跟reverse不同原理不同。sort()不帶參數的時候是從字面上對數組排序    var arr = [0, 1, 5, 10, 15];    arr.sort();    console.log(arr); //0,1,10,15,5 ,注意這裡是字串按ASCII進行比較的    //調用這樣的函數就按數值方式排列了    function sortNumber(a, b) {        return a - b    }    arr.sort(sortNumber);    console.log(arr);   //0,1,5,10,15    //slice(start,end):返回從原數組中指定start到end(不包含該元素)之間的項組成的新數組,如只有一個參數,則從start到數組末尾    var arr = [1, 2, 3, 4, 5, 6, 7, 8];    var out = arr.slice(2,3);    console.log(arr); //[1, 2, 3, 4, 5, 6, 7, 8]    console.log(out); //[3]    //join(separator):將數組的元素組成一個字串,以separator為分隔字元,省略的話則用預設用逗號為分隔字元    var arr = [1, 2, 3, 4, 5, 6];    var out = arr.join();    console.log(arr); //[1, 2, 3, 4, 5, 6]    console.log(out); //1,2,3,4,5,6    out = arr.join("");    console.log(out); //123456    out = arr.join("&");    console.log(out); //1&2&3&4&5&6</script>

 

相關文章

聯繫我們

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