//定義一個數組
var arr = [1,2,3,5,9,5,7,3]
前增 數組
console.log(arr.unshift(000)) //9
console.log(arr) //[0, 1, 2, 3, 5, 9, 5, 7, 3]
改變原數組 返回值是數組的長度spa
console.log(arr.push(0)); //9
console.log(arr) //[1, 2, 3, 5, 9, 5, 7, 3, 0]
改變原數組 返回值是數組的長度code
console.log(arr.shift()) //1
console.log(arr) //[2, 3, 5, 9, 5, 7, 3]
改變原數組 返回值是刪除後的數組blog
console.log(arr.pop()); //3
console.log(arr); //[1, 2, 3, 5, 9, 5, 7]
改變原數組 返回值是刪除後的數組字符串
//參數只有一個,表明從前保留的數量
console.log(arr.splice(2)) //[3, 5, 9, 5, 7, 3]
console.log(arr) //[1, 2]
//參數兩個,第一個表明從哪一個下標開始,第二個表明數量
console.log(arr.splice(2,2)) //[3, 5]
console.log(arr) //[1, 2, 9, 5, 7, 3]
//參數是三個,表明
console.log(arr.splice(0,2,1)) //[1, 2]
console.log(arr) //[1, 3, 5, 9, 5, 7, 3]
改變原數組console
console.log(arr.slice(1,5)); //[2, 3, 5, 9]
console.log(arr); //[1, 2, 3, 5, 9, 5, 7, 3]
不改變原數組class
參數di
//參數不寫
console.log(arr.join()); //1,2,3,5,9,5,7,3
console.log(arr) //[1, 2, 3, 5, 9, 5, 7, 3]
//參數是空
console.log(arr.join("")); //12359573
console.log(arr) //[1, 2, 3, 5, 9, 5, 7, 3]
//參數是 -
console.log(arr.join("-")); //1-2-3-5-9-5-7-3
console.log(arr) //[1, 2, 3, 5, 9, 5, 7, 3]
指定字符隔開數組的每一個值 不改變原數組co
console.log(arr.concat([2,3,4])); //[1, 2, 3, 5, 9, 5, 7, 3, 2, 3, 4]
console.log(arr); //[1, 2, 3, 5, 9, 5, 7, 3]
能夠是任何類型 不改變原數組join
參數
console.log(arr.reverse()) //[3, 7, 5, 9, 5, 3, 2, 1]
console.log(arr) //[3, 7, 5, 9, 5, 3, 2, 1]
改變原數組