When creating a new object from an existing object, we might want to exclude some properties. Instead of using loops, we can use destructuring and the rest operator.


Suppose we have the following object:

const user = {
    firstname: 'Peter',
    lastname: 'Piper',
    password: '*********',
    active: true,
    position_id: 23,
    department_id: 46

We want to exclude the password, position_id, and department_id properties when creating a new user object called userObj. As stated above, we use destructuring and the rest operator:

const {
} = user;

If we console.log(userObj), we get the following output:

{firstname: "Peter", lastname: "Piper", active: true}