Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use the spread operator (three dots) except specific keys?

Tags:

javascript

Is the spread operator have the exception feature? like:

  originalObject = {
    key1: '',
    key2: '',
    key3: ''
  }

  const clonedOriginalObject = {
    ...object,
    // except key2
  }

So what I'm excepting is the clonedObject contains only key1 and key2

like image 717
Khaled Ramadan Avatar asked Oct 17 '25 19:10

Khaled Ramadan


1 Answers

you can use Destructuring_assignment to omit key2 and get the rest :

originalObject = {
  key1: "",
  key2: "",
  key3: ""
};

const { key2, ...clonedOriginalObject } = originalObject;

console.log(clonedOriginalObject);
like image 86
Taki Avatar answered Oct 19 '25 09:10

Taki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!