There are two ways to delete a property from an object.
- Normal way, with
delete
operator - Easy and Better way, with rest operator
delete operator
const person = {
name: "John Doe",
age: 30,
};
delete person.age;
console.log(person);
// { name: "John Doe" }
rest operator
const person = {
name: "John Doe",
age: 30,
};
const { age, ...newPerson } = person;
console.log(newPerson);
// { name: "John Doe" }
Why removing a property with rest operator is better?
delete
operator mutates the original object, while using rest operator you make a new copy of the object without given property.
If you enjoyed this post please share, and follow me on DEV.to and Twitter if you would like to know as soon as I publish a post! 🔥