How do I remove a property from a JavaScript object?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

Removing a property from a JavaScript object is a common task when managing data structures in web development. You can accomplish this in several ways, depending on the specific requirements of your project and the JavaScript environment you are working in. Here are some methods to remove a property from an object in JavaScript:

Using the delete Operator

The most straightforward method to remove a property from an object is by using the delete operator. This operator removes a property from an object if it exists and will not throw an error if the property does not exist.

let person = { name: "John", age: 30, occupation: "Developer" }; delete person.occupation; // Removes the 'occupation' property console.log(person); // Output: { name: "John", age: 30 }

The delete operator modifies the object in place and returns true if the operation is successful, except when the property is non-configurable, in which case it returns false.

Using Destructuring and Rest Properties (ES2018+)

For scenarios where you need a new object without certain properties (without mutating the original object), you can use object destructuring along with the rest properties introduced in ES2018.

let person = { name: "John", age: 30, occupation: "Developer" }; let { occupation, ...newPerson } = person; console.log(newPerson); // Output: { name: "John", age: 30 } console.log(person); // Output: { name: "John", age: 30, occupation: "Developer" }

In this example, occupation is extracted, and newPerson contains all the remaining properties. This technique is beneficial when you want to preserve the original object and create a new object that lacks certain properties.

Using Object.assign or Spread Operator for Filtering Out Properties

If you're dealing with older JavaScript environments that don't support rest properties in destructuring, you can achieve similar results using Object.assign() or the spread operator in combination with delete:

let person = { name: "John", age: 30, occupation: "Developer" }; // Using Object.assign let clonedPerson = Object.assign({}, person); delete clonedPerson.occupation; console.log(clonedPerson); // Output: { name: "John", age: 30 } // Or using the spread operator let anotherClonedPerson = { ...person }; delete anotherClonedPerson.occupation; console.log(anotherClonedPerson); // Output: { name: "John", age: 30 }

Both methods involve creating a shallow copy of the original object and then deleting the unwanted property from the copy.

Considerations

  • The delete operator is generally the way to go if you need to remove a property from an object in place.
  • If you need to create a new object without certain properties and are working in an environment that supports ES2018+, use destructuring with rest properties as it's more succinct and avoids direct mutation.
  • Be mindful of the object's property descriptor if using delete. If a property is configured as non-deletable, delete will not remove it.

Choosing the right method depends on whether you want to mutate the original object or create a new object without certain properties, and what JavaScript features are supported in your working environment.

TAGS
System Design Interview
CONTRIBUTOR
Design Gurus Team
Explore Answers
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking 75: Top Coding Interview Questions