『壹』 js中刪除數組或對象
在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