Home >Web Front-end >JS Tutorial >How Do I Remove Properties from JavaScript Objects?
Removing Properties from JavaScript Objects
To remove a property from a JavaScript object, we leverage the delete operator.
Usage:
The delete operator allows you to delete specific properties from an object. You can use it in several ways:
Using Dot Notation:
delete myObject.propertyName;
Using Bracket Notation:
delete myObject['propertyName'];
Assigning to 'undefined':
This approach is not recommended but still possible.
var propName = 'propertyName'; myObject[propName] = undefined; delete myObject[propName];
Example:
Consider the following object:
let myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" };
To remove the regex property, you can use any of the methods mentioned above:
delete myObject.regex; console.log(myObject); // { ircEvent: 'PRIVMSG', method: 'newURI' }
Using the delete operator ensures that the property is effectively removed from the object.
The above is the detailed content of How Do I Remove Properties from JavaScript Objects?. For more information, please follow other related articles on the PHP Chinese website!