Clue Mediator

Remove a property from an object in JavaScript

πŸ“…November 23, 2021
πŸ—JavaScript

Today we will show you how to remove a property from an object in JavaScript. We will see several methods with examples.

Checkout more articles on JavaScript

  • json-data-structure-from-a-json-object" title="Get a JSON data structure from a JSON object">Get a JSON data structure from a JSON object
  • Navigator object in JavaScript
  • array-in-javascript" title="Sort and group objects alphabetically by the first letter from an array in JavaScript">Sort and group objects alphabetically by the first letter from an array in JavaScript
  • string-from-the-dynamic-object-in-javascript" title="Trim string from the dynamic object in JavaScript">Trim string from the dynamic object in JavaScript

Remove a property from an object (mutating the object)

You can do it like this:

delete myObject.foo;
// or
delete myObject['foo'];
// or,
var prop = "foo";
delete myObject[prop];

Example:

var myObject = {
    "foo": "123",
    "bar": "456",
    "baz": "789"
};
delete myObject.foo;

console.log(myObject);
// Output: { "bar": "456", "baz": "789" }

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! πŸ™‚