var arr = ['a', 'b', 'c', '1', '2', '3'];
在數組的末尾增長一個或多個元素,並返回數組的新長度 `console.log(arr.push('x', 'x' ,'x'))` // 9 `arr.push([2])` // [2] 會被當作一項 即:arr[6] 是 [2]
刪除數組的最後一個元素,並返回這個元素 `console.log(arr.pop());` // 3
在數組的開頭添加一個或者朵兒元素,並返回數組新的 length 值 `console.log(arr.unshift())`
刪除數組的第一個元素, 並返回這個元素 `console.log(arr.shift());` //a
將數組中的全部元素鏈接成一個字符串。 `console.log(arr.join());` //a, b, c, 1, 2, 3
將傳入的數組或非數組值與原數組合並,組成一個新的數組並返回 `console.log(arr.concat(0, 9));` // ['a', 'b' ,'c', '1', '2', '3', 0, 9] `console.log(arr.concat([0, 9]));` // ['a', 'b' ,'c', '1', '2', '3', 0, 9]` `console.log(arr.concat([[9]]));` // ['a', 'b' ,'c', '1', '2', '3', 0, [9]]`
對數組的元素作原地的排序, 並返回這個數組。 arr = [2, 0, 1, 6]; console.log(arr.sort()); // [0, 1, 2, 6] arr.sort(function(a, b)) { console.log(a + '-' + b); return a - b; // 從小到大 return a + b ; // 從大到小 } console.log(arr);
把數組中一部分的錢復制存入一個新數組對象中,並返回這個新的數組 arr = [{a: 9}]; var ret = arr.slice(0, 1); console.log(ret); // [{a:9}] ret[0].a = 10; console.log(arr[0].a); // 10
用新元素替換舊元素,以此修改數組的內容 參數: 第一個參數: 表示開始位置 第二個參數: 長度 剩餘參數: 要添加到數組中的元素 `console.log(arr.splice(3, 3));` // ['1', '2', '3']
filter是指能夠刪除數組中的某一項元素。數組
var arr = [1,2,3,4,5]; var newArr = arr,filter(function(item){ if(item == 4) { return false; } else { return true; } }) console.log(newArr);// 1, 2, 3, 5
reverse() 是指能夠顛倒數組中元素的順序code
var arr = [1, 2, 3, 4, 5]; arr.reverse(); 獲得新的數組就是 [5, 4, 3, 2, 1];