DEV Community

Fatimah for Summitech Computing Ltd

Posted on

3 2

Mass delete properties from a Javascript object

Suppose you had an object like this:

const person = {name: 'Obama', age: 58, occupation: 'Statesperson', country: 'America', gender: 'male', marital_status: 'married'};

and you'd like to delete the following properties: marital_status, occupation and gender.

What you'd probably do (but hate) is:

delete person.marital_status;
delete person.occupation;
delete person.gender;

This is obviously tedious. A better way will be to use ES6's destructuring syntax:

const {marital_status, gender, occupation, ...others} = person;

console logging "others" will give you the person object without marital_status, occupation and gender.

{name: 'Obama', age: 58, country: 'America'};

One cool thing to note is that the order of properties is not important, however, the new variable (in this case, others) must be at the end with the three ellipsis.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series πŸ“Ί

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series πŸ‘€

Watch the Youtube series

πŸ‘‹ Kindness is contagious

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

Okay