Home >Web Front-end >JS Tutorial >How Do I Remove a Property from a JavaScript Object?
In JavaScript, you can dynamically modify an object by adding or removing properties. This article focuses on the task of removing a specific property from an object.
Consider the following JavaScript object:
let myObject = { "ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*" };
The goal is to remove the "regex" property from this object to obtain the following result:
let myObject = { "ircEvent": "PRIVMSG", "method": "newURI" };
To remove a property from an object, you can use the delete keyword. You can specify the property to remove in three ways:
Demonstration:
delete myObject.regex; console.log(myObject); // Logs the modified object without the "regex" property
Output:
{ ircEvent: 'PRIVMSG', method: 'newURI' }
The above is the detailed content of How Do I Remove a Property from a JavaScript Object?. For more information, please follow other related articles on the PHP Chinese website!