9

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?

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
doobean
  • 1,929
  • 4
  • 19
  • 27

1 Answers1

6

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.

Zoidberg
  • 10,137
  • 2
  • 31
  • 53
  • Based on the question, it seemed the person asking simply wanted to remove the single property from an object. Communication is key, I would ask why they want to destructure in the first place when something simpler would do. – Zoidberg Nov 17 '19 at 23:48