How to remove a property from a JavaScript object?
Javascript | Object | delete | how to
Graduated with a Master’s degree in Software Engineering from San Jose State University in December 2019 and currently working at PayPal on the Customer Journey Platform. I enjoy generating new ideas and devising feasible solutions to broadly relevant problems. My colleagues would describe me as a driven, resourceful individual who maintains a positive, proactive attitude when faced with adversity. Interested to work in areas related to Distributed Systems, Cloud Infrastructure and Micro-Services. I get a lot of satisfaction from the constant learning and puzzle solving that comes with my profession and contributing to a successful and worthwhile product. My expertise includes project design and management, data analysis and interpretation, and the development and implementation of software products.
Given an object:
let myObject = {
"property": "value",
"another-property": "another-value"
};
To remove a property from an object (mutating the object), you can do it like this:
delete myObject.property; // true
// or,
delete myObject['property']; // true
// or,
var prop = "property";
delete myObject[prop]; // true
delete myObject.invalidproperty; // false
The delete operator is used to remove these keys, more commonly known as object properties, one at a time. delete operator returns false when a property can not be deleted.
Note: Here's the blog to understand delete operator in-depth. It is highly recommended.