How to remove a property from a JavaScript object?
Javascript | Object | delete | how to
Given an object:
let myObject = {
"property": "value",
"another-property": "another-value"
};
To remove a property from an object (mutating the object), you can do it like this:
delete myObject.property; // true
// or,
delete myObject['property']; // true
// or,
var prop = "property";
delete myObject[prop]; // true
delete myObject.invalidproperty; // false
The delete
operator is used to remove these keys, more commonly known as object properties, one at a time. delete
operator returns false
when a property can not be deleted.
Note:
Here's the blog to understand delete operator in-depth. It is highly recommended.