Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to destructure an object without one key [duplicate]

I have an object like obj = { test1: 'sth', test2: 'sth', label: 'sth' }.

And I would like to destructure this object {...obj} except label to get { test1: 'sth', test2: 'sth' }.

How to destructure the object without this key?

Should I create a new object or is there any way to do this simply in one line?

like image 876
doobean Avatar asked Nov 17 '19 20:11

doobean


People also ask

Can you Destructure a nested object?

Destructuring nested objectsIf we need to access an employee's info we can destructure as many levels as it takes to get to our employee object's properties. const { engineers: { 1: { id, name, occupation, }, }, } = employees; Now we have access to all of the second employee object's properties.


1 Answers

Simple delete should do the trick.

delete obj.label;

EDIT: apparently my question did not do destructuring properly. Perhaps something like the following would work then.

({label, ...rest} = {test1: 'sth', test2: 'sth', label: 'sth' });
console.debug(rest);

Rest should contain only test1 and test2 properties/values.

like image 113
Zoidberg Avatar answered Oct 09 '22 07:10

Zoidberg