js数组的插入和删除

1、你需要添加一个或多个要添加到数组末尾的元素push。

2、从数组中删除最后一个元素的话直接使用 pop() 就可以。

3、unshift() 和 shift() 从功能上与 push() 和 pop()只是它们分别作用于数组的开始,而不是结尾。

var myArray = ['Manchester', 'London', 'Liverpool', 'Birmingham', 'Leeds', 'Carlisle']
myArray.push('张三')
console.log(myArray)  //["Manchester", "London", "Liverpool", "Birmingham", "Leeds", "Carlisle", "张三"]
        
myArray.pop()
console.log(myArray)  //["Manchester", "London", "Liverpool", "Birmingham", "Leeds", "Carlisle"]

myArray.unshift('Edinburgh');
console.log(myArray)  //["Edinburgh", "Manchester", "London", "Liverpool", "Birmingham", "Leeds", "Carlisle"]

myArray.shift()
console.log(myArray)  //["Manchester", "London", "Liverpool", "Birmingham", "Leeds", "Carlisle"]

 

你可能感兴趣的:(ES6)