DEV Community

Cover image for How To: Delete property from an Object using spread operator
Calvin Torra
Calvin Torra

Posted on • Edited on • Originally published at calvintorra.com

4 1

How To: Delete property from an Object using spread operator

Join my newsletter Here:

Once a week, I share my best finds on:

  • AWS
  • JavaScript
  • Web Scraping
  • Indie Hacking

Original Post and more Here

Everyday there’s something new to learn with Javascript.

I was trying to manipulate an object and remove one of the properties but I didn’t want to mutate the original object. I knew there must be a cleaner way than to use the delete operator.

That got me thinking about the spread operator and it turns out you can remove properties while spreading the rest of the values into a new object.

let user = {
    name: 'Calvin',
    age: 200,
    country: 'Spain',
    food: 'Pizza'
}

const {name, ...restOfUser} = user

console.log(restOfUser)
console.log(name)

// { age: 200, country: 'Spain', food: 'Pizza' }
// Calvin
Enter fullscreen mode Exit fullscreen mode

I now get the removed property value and also a new object with all the rest of the values.

Join my newsletter Here:

Once a week, I share my best finds on:

  • AWS
  • JavaScript
  • Web Scraping
  • Indie Hacking

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay