數組是一個容器能夠存儲一系列的值。下面是數組中很重要的幾個方法。 數組
var a1 = ["hh","xixi","gg"]; var a2 = ["ni", "hao", "ma"]; a1.concat(a2);//返回["hh", "xixi", "gg", "ni", "hao", "ma"]
var a2 = ["ni", "hao", "ma"]; a2.join("-");//返回ni-hao-ma"
var a3 = [13,14,520]; a3.pop();//返回520 a3;//返回[13, 14]
用法簡介:該方法可向數組的末尾添加一個或多個元素,並返回新的長度。
例子: 函數
var a4 = ["How","are"]; a4.push("you", "today", "?");//返回5 a4;//返回["How", "are", "you", "today", "?"]
var a5 = ["How", "are", "you", "today", "?"] a5.reverse();//返回["?", "today", "you", "are", "How"]
var a6 = ["?", "today", "you", "are", "How"]; a6.shift();//返回"?" a6;//返回["today", "you", "are", "How"]
var a7 = [1,2,3,4,5,6,7]; a7.slice(0,5);//選取的是從數組的第1個元素到第5個元素,返回[1, 2, 3, 4, 5] a7.slice(-3);//選取的是從數組的倒數第3個元素到最後一個元素,返回[5, 6, 7] a7.slice(-5,-1)//選取的是從數組的倒數第5個元素到倒數第二個元素,返回[3, 4, 5, 6] a6;//返回["today", "you", "are", "How"]
var a8 = [1,2,3,4,5,6,7,8,9]; a8.splice(0,3);//[1, 2, 3] a8;//[4, 5, 6, 7, 8, 9] a8.splice(0,0,1,2,3)//[] a8;//[1, 2, 3, 4, 5, 6, 7, 8, 9] a8.splice(1,0,11);//[] a8; //[1, 11, 2, 3, 4, 5, 6, 7, 8, 9] a8.splice(1,3);//[11, 2, 3] a8;;//[1, 4, 5, 6, 7, 8, 9]
var a9 = [4,5,6,7,8,9]; a9.unshift(1,2,3)//9 a9;//[1, 2, 3, 4, 5, 6, 7, 8, 9]
var a10 = [6,2,1,3,4,7,9,8,5]; a10.sort();//[1, 2, 3, 4, 5, 6, 7, 8, 9] function sortNumber(a,b) { return a - b } var a10 = [6,2,1,3,4,7,9,8,5]; a10.sort(sortNumber);//[1, 2, 3, 4, 5, 6, 7, 8, 9]