Home >Web Front-end >JS Tutorial >How Do I Delete a Property from a JavaScript Object?
Deleting a Property from a JavaScript Object
In JavaScript, objects are collections of key-value pairs. Deleting a property from an object removes the key-value pair associated with that property.
How to Delete a Property
There are several ways to delete a property from an object:
delete myObject.regex; // Remove "regex" property
delete myObject['regex']; // Remove "regex" property
const prop = "regex"; delete myObject[prop]; // Remove "regex" property
Example
Given the following object:
const myObject = { ircEvent: "PRIVMSG", method: "newURI", regex: "^http://.*" };
To remove the "regex" property and end up with the following object:
const myObject = { ircEvent: "PRIVMSG", method: "newURI" };
You can use the following code:
delete myObject.regex; console.log(myObject); // Outputs: {ircEvent: "PRIVMSG", method: "newURI"}
The above is the detailed content of How Do I Delete a Property from a JavaScript Object?. For more information, please follow other related articles on the PHP Chinese website!