opop()
oshift()
osplice()
oslice()
1、pop()
pop() 方法用于删除数组的最后一项,同时减少数组的length 值,返回被删除的项
let colors = ["red", "green"]
let item = colors.pop()// 取得最后一项
console.log(item) // green
console.log(colors.length) // 1
2、shift()
shift()方法用于删除数组的第一项,同时减少数组的length 值,返回被删除的项
let colors = ["red", "green"]
let item = colors.shift()// 取得第一项
console.log(item) // red
console.log(colors.length) // 1
3、splice()
传入两个参数,分别是开始位置,删除元素的数量,返回包含删除元素的数组
let colors = ["red", "green", "blue"]
let removed = colors.splice(0,1)// 删除第一项
console.log(colors)// green,blue
console.log(removed)// red,只有一个元素的数组
4、slice()
slice() 用于创建一个包含原有数组中一个或多个元素的新数组,不会影响原始数组
let colors = ["red", "green", "blue", "yellow", "purple"]
let colors2 = colors.slice(1)
let colors3 = colors.slice(1, 4)
console.log(colors) // red,green,blue,yellow,purple
concole.log(colors2)// green,blue,yellow,purple
concole.log(colors3)// green,blue,yellow
在vue中使用vue.delete()
删除对象属性
通过delete操作符, 可以实现对对象属性的删除操作, 返回值是布尔
例: var obj={name: 'zhagnsan',age: 19 }
delete obj.name //true
typeof obj.name //undefined
同样可用于函数,数组,变量,对象,但对象不能删除,只能做到删除对象属性
删除变量
例: var name ='zs' //已声明的变量
delete name //false
console.log(typeof name) //String
age = 19 //未声明的变量
delete age //true
typeof age //undefined
this.val = 'fds' //window下的变量
delete this.val //true
console.log(typeof this.val) //undefined
删除数组
以声明数组返回false,未声明返回true
var arr = ['1','2','3'] ///已声明的数组
delete arr //false
console.log(typeof arr) //object
arr = ['1','2','3'] //未声明的数组
delete arr //true
console.log(typeof arr) //undefined
var arr = ['1','2','3'] //已声明的数组
delete arr[1] //true
console.log(arr) //['1','empty','3']
删除函数
var fn = function(){} //已声明的函数
delete fn //false
console.log(typeof fn) //function
fn = function(){} //未声明的函数
delete fn //true
console.log(typeof fn) //undefined
删除对象
var person = {
height: 180,
long: 180,
weight: 180,
hobby: {
ball: 'good',
music: 'nice'
}
}
delete person ///false
console.log(typeof person) //object
var person = {
height: 180,
long: 180,
weight: 180,
hobby: {
ball: 'good',
music: 'nice'
}
}
delete person.hobby ///true
console.log(typeof person.hobby) //undefined