一、添加
1、push() 方法將一個或多個元素添加到數(shù)組的末尾,并返回數(shù)組的新長度。
2、unshift() 方法將一個或多個元素添加到數(shù)組的開頭,并返回新數(shù)組的長度。
二、刪除
3、pop()方法從數(shù)組中刪除最后一個元素,并返回該元素的值。此方法更改數(shù)組的長度。
4、shift() 方法從數(shù)組中刪除第一個元素,并返回該元素的值。此方法更改數(shù)組的長度。
三、操作
5、concat() 方法用于合并兩個或多個數(shù)組。此方法不會更改現(xiàn)有數(shù)組,而是返回一個新數(shù)組。
var color = ['red','green','blue'];
var color2 = color.concat('yellow',['black','white']);
console.log(color2) //red,green,blue,yellow,black,white
6、slice()方法返回一個從開始到結(jié)束(不包括結(jié)束)選擇的數(shù)組的一部分淺拷貝到一個新數(shù)組對象,原始數(shù)組不會被修改。
var colors = ['red','green','blue','black','white'];
var colors2 = colors.slice(1) //green,blue,black,white
var colors3 = colors.slice(1,4) //green,blue,black
7、splice()方法始終會返回一個數(shù)組,該數(shù)組包含從原始數(shù)組中刪除的項(如果沒有刪除任何項,則返回一個空數(shù)組),用途最廣,有如下3種
刪除:需指定2個參數(shù),要刪除的第一項位置和要刪除的項數(shù)
插入:需提供3個參數(shù),起始位置、0(要刪除的項數(shù))和要插入的項,如要插入多個項 ,再傳入第四,五...
替換:需指定3個參數(shù),起始位置、要刪除的項數(shù)和要插入的任意數(shù)量的項
var colors = ['red','green','blue'];
var removed = colors.splice(0,1);
console.log(colors); //green,blue
console.log(removed); //red
var removed = colors.splice(1,0,'black');
console.log(colors); //green,black,blue
console.log(removed); // 返回空數(shù)組
var removed = colors.splice(0,2,'yellow','white');
console.log(colors); //yellow,white,blue
console.log(removed); //red,green
7、join(); 使用指定的字符串用來分隔數(shù)組字符串
8、splice():主要用法是向數(shù)組的中部插入項,會改變原數(shù)組,是最強大的數(shù)組方法。最多指定3個參數(shù)。
9、reverse():顛倒所有數(shù)組元素。
10.String() 函數(shù)把對象的值轉(zhuǎn)換為字符串。