<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>