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
- Get a JSON data structure from a JSON object
- Navigator object in JavaScript
- Sort and group objects alphabetically by the first letter from an array 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..!! π