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
1、splice
splice(index,len,[item])注释:该方法会改变原始数组。
splice有3个参数,它也可以用来替换/删除/添加数组内某一个或者几个值
index:数组开始下标len: 替换/删除的长度 item:替换的值,删除操作的话 item为空
如:arr = ['a','b','c','d']
删除 ---- item不设置
arr.splice(1,1) //['a','c','d'] 删除起始下标为1,长度为1的一个值,len设置的1,如果为0,则数组不变
arr.splice(1,2) //['a','d'] 删除起始下标为1,长度为2的一个值,len设置的2
替换 ---- item为替换的值
arr.splice(1,1,'ttt')//['a','ttt','c','d'] 替换起始下标为1,长度为1的一个值为‘ttt',len设置的1
arr.splice(1,2,'ttt')//['a','ttt','d'] 替换起始下标为1,长度为2的两个值为‘ttt',len设置的1
添加 ---- len设置为0,item为添加的值
arr.splice(1,0,'ttt')//['a','ttt','b','c','d'] 表示在下标为1处添加一项‘ttt'
看来还是splice最方便啦
2、delete
delete删除掉数组中的元素后,会把该下标出的值置为undefined,数组的长度不会变
如:delete arr[1] //['a', ,'c','d'] 中间出现两个逗号,数组长度不变,有一项为undefined