理順 JavaScript (11) – 數組

來源:互聯網
上載者:User
數組直接量
var arr;arr = ['AA', 'BB', 'CC'];alert(arr.toLocaleString()); //AA, BB, CCarr = [11, 22, 33];alert(arr.toLocaleString()); //11.00, 22.00, 33.00/* 數組元素可以是任意類型, 包括數組本身 */arr = ['AA', 'BB', 123];alert(arr.toLocaleString()); //AA, BB, 123.00alert(arr[0]);               //AAalert(arr[1]);               //BBalert(arr[2]);               //123/* 可先站定維數, 以後賦值 */arr = [ , , ];arr[0] = 11;arr[1] = 22;arr[2] = 33;alert(arr.toLocaleString()); //11.00, 22.00, 33.00/* 先給個空數組, 再指定維數 */arr = [];alert(arr.length);           //0arr.length = 3;arr[0] = 11;arr[1] = 22;arr[2] = 33;alert(arr.toLocaleString()); //11.00, 22.00, 33.00/* 可用 length 屬性改變數組的大小 */arr = [1,2,3,4,5,6];arr.length = 2;alert(arr.toLocaleString()); //1.00, 2.00

數組對象的三種建立方法

var arr;arr = new Array(11, '22', true);alert(arr.toLocaleString()); //11.00, 22, truearr = new Array(3);arr[0] = 11;arr[1] = '22';arr[2] = true;alert(arr.toLocaleString()); //11.00, 22, truearr = new Array();arr.length = 3;arr[0] = 11;arr[1] = '22';arr[2] = true;alert(arr.toLocaleString()); //11.00, 22, true

join : 數組到字串

var arr, str;arr = new Array('a', 'b', 1, 2, 3);str = arr.join();alert(str); //a,b,1,2,3/* 可指定分隔字元 */str = arr.join(' * ');alert(str); //a * b * 1 * 2 * 3

reverse、sort : 倒置與排序

var arr;arr = new Array('c', 'd', 'e', 'a', 'b');arr.reverse();alert(arr.toLocaleString()); //b, a, e, d, carr.sort();alert(arr.toLocaleString()); //a, b, c, d, e

concat : 添加返回新數組

var arr;arr = [1, 2, 3]arr = arr.concat(4, 5);alert(arr.join()); //1, 2, 3, 4, 5

push、pop : 從尾部添加與刪除; push 返回新長度, pop 返回被刪除的元素

var arr;arr = [1, 2, 3, 4, 5]arr.pop();alert(arr.join()); //1,2,3,4arr.push(7, 8, 9);alert(arr.join()); //1,2,3,4,7,8,9arr.push(0, ['A', 'B']);alert(arr.join()); //1,2,3,4,7,8,9,0,A,Barr.pop();alert(arr.join()); //1,2,3,4,7,8,9,0arr.pop();alert(arr.join()); //1,2,3,4,7,8,9

unshift、shift : 從開始插入與刪除; unshift 返回新長度, shift 返回被刪除的元素

var arr;arr = [1, 2, 3]arr.unshift(5, 6);alert(arr.join()); //5,6,1,2,3arr.shift();arr.shift();alert(arr.join()); //1,2,3arr.unshift([5,6,7,8]);alert(arr.join()); //5,6,7,8,1,2,3arr.shift();alert(arr.join()); //1,2,3

splice : 刪除、替換、插入; 如果有刪除則返回被刪除元素的數組

var arr;arr = [1, 2, 3, 4, 5, 6, 7, 8]arr.splice(4, 3); //從第 4 個(0起)開始刪除 3 個alert(arr.join()); //1, 2, 3, 4, 8arr.splice(3, 3, 'A', 'B', 'C'); //從第 3 個(0起)開始替換 3 個alert(arr.join()); //1, 2, 3, A, B, Carr.splice(3, 0, 7, 8, 9); //從第 3 個(0起)開始插入 3 個; 插入時第二個參數給 0alert(arr.join()); //1, 2, 3, 7, 8, 9, A, B, C

slice : 提取子數組; 返回新數組

var arr;arr = [1, 2, 3, 4, 5, 6, 7, 8]arr = arr.slice(2, 5);alert(arr.join()); //3,4,5/* 參數為負表示從後面數 */arr = [1, 2, 3, 4, 5, 6, 7, 8]arr = arr.slice(2, -2);alert(arr.join()); //3,4,5,6arr = [1, 2, 3, 4, 5, 6, 7, 8]arr = arr.slice(-3, -1);alert(arr.join()); //6,7

toString、valueOf

var arr;arr = [1, 2, 3, 'A', 'B', 'C']alert(arr);                  //1,2,3,A,B,Calert(arr.toString());       //1,2,3,A,B,Calert(arr.valueOf());        //1,2,3,A,B,Calert(arr.toLocaleString()); //1.00, 2.00, 3.00, A, B, C

遍曆與類比的多維陣列

var arr;arr = [[1,2,3], [4,5,6], [7,8,9]];alert(arr[1][1]); //5arr = new Array(new Array(1,2,3), new Array(4,5,6), new Array(7,8,9));alert(arr[1][1]); //5alert(arr) //1,2,3,4,5,6,7,8,9for (i=0; i<arr.length; i++) {  document.write(arr[i] + '<b>'); //1,2,3 / 4,5,6 / 7,8,9}arr = [1,2,3,4,5,6,7,8,9];for (i in arr) {  document.write(arr[i] + '<br>'); //1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9}arr = new Array([1,2,3], [4,5,6], [7,8,9]);for (i in arr) {  document.write(arr[i] + '<br>'); //1,2,3 / 4,5,6 / 7,8,9}for (i in arr) for (j in arr[i])  document.write(arr[i][j] + '<br>'); //1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9

相關文章

聯繫我們

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