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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3