js 向数组前、后 添加元素

将新项添加到数组-起始位置:

unshift()

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple");

fruits 将输出:

Lemon,Pineapple,Banana,Orange,Apple,Mango

unshift() 方法可向数组的开头添加一个或更多元素,并返回新的长度。


将新项添加到数组-末尾

push()

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Lemon","Pineapple");

fruits 将输出:

Banana,Orange,Apple,Mango,Lemon,Pineapple


简单记录

你可能感兴趣的:(js,javascript)