"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Do I Remove Properties from JavaScript Objects?

How Do I Remove Properties from JavaScript Objects?

Published on 2025-02-03
Browse:964

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.

Latest tutorial More>

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