添加push()、unshift()、splice()、concat()
push()方法接收任意數(shù)量的參數(shù),并將它們添加到數(shù)組末尾,返回?cái)?shù)組的最新長(zhǎng)度
letcolors?=?[];//?創(chuàng)建一個(gè)數(shù)組
letcount?=?colors.push("red","green");//?推入兩項(xiàng)
console.log(count)//?
unshift()在數(shù)組開頭添加任意多個(gè)值,然后返回新的數(shù)組長(zhǎng)度
letcolors?=newArray();//?創(chuàng)建一個(gè)數(shù)組
letcount?=?colors.unshift("red","green");//?從數(shù)組開頭推入兩項(xiàng)
alert(count);//?
splice()傳入三個(gè)參數(shù),分別是開始位置、0(要?jiǎng)h除的元素?cái)?shù)量)、插入的元素,返回空數(shù)組
letcolors?=?["red","green","blue"];
letremoved?=?colors.splice(1,0,"yellow","orange")
console.log(colors)//?red,yellow,orange,green,blue
console.log(removed)//?[]
concat()首先會(huì)創(chuàng)建一個(gè)當(dāng)前數(shù)組的副本,然后再把它的參數(shù)添加到副本末尾,最后返回這個(gè)新構(gòu)建的數(shù)組,不會(huì)影響原始數(shù)組
letcolors?=?["red","green","blue"];
letcolors2?=?colors.concat("yellow",?["black","brown"]);
console.log(colors);//?["red",?"green","blue"]
console.log(colors2);//?["red",?"green",?"blue",?"yellow",?"black",?"brown"]
刪除 pop()、shift()、splice()、slice()