Asked 7 years ago
5 Jan 2017
Views 1536
Rasi

Rasi posted

How to remove a property from a JavaScript object?

how to remove a property from a JavaScript Object ?

example object ::

var obj = {
accessKey : ""
, length : 2
, selector : ".nav"
}


need to remove element selector from a JavaScript Object , or some time alter value / remove value of a property from a JavaScript Object ?
how we can remove property of JavaScript Object with best coding standard , with best way so it give us good performance for removing thousands of the Object ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

delete operator will do your job.
delete operator delete property of object.


var obj = {
accessKey : "" ,
length : 2 ,
selector : ".nav"
}
delete obj.selector ; // return true and remove object property.
[code]

delete obj.selector 
will remove selector property of obj(object)

you can also use like ::   delete(syntax)

[code]
delete (obj.selector) // return true and remove object property.


when delete operator return true . it means property got deleted. if delete operator return false means property is not deleted.

var d=1;
delete d ; // return false . 


more detail at mdn
Post Answer