Omit properties from an object
Published about 6 years ago
Here is a cool method for omitting properties from an object using the ES7 Object Spread Operator
We are destructuring the user object to store password and address inside a constant and then grabbing whatever remains into another constant labelled safeUser which has the password and address properties omitted 🤜🤛
With this pattern we aren't mutating or modifying the existing user object. We are removing the reference to certain properties.
const user = {
id: 12345,
password: "password123",
address: "123 Ocean Drive",
name: "Billy Ocean",
age: 80,
status: "Legend",
};
const { password, address, ...safeUser } = user;
console.log(safeUser);
// { id: 12345, name: 'Billy Ocean', age: 18, status: 'Legend' }