d
Amit DhamuSoftware Engineer
 

Omit property from object

1 minute read 00000 views

Take our dead simple object below.

const fridge = {
  milk: 2,
  eggs: 12,
  cheese: 1,
}

If we wanted to get the object without the milk property, we can use object destructuring like so:

const { milk, ...fridgeWithoutMilk } = fridge

console.log(fridgeWithoutMilk);

// returns
{ eggs: 12, cheese: 1 }

Doing this also means we don't mutate the original object.

console.log(fridge)

// returns
{ milk: 2, eggs: 12, cheese: 1 }