2

I am trying to delete multiple computed properties from an object at once using destructuring. Something like this

const a = {b: 1, c: 2 , d: 3};
const forbiddenKeys = [ "b", "c"]; // pretend this is computed
const { ...forbiddenKeys, ...rest } = a; // gives "Uncaught SyntaxError: Rest element must be last element"

My plan was to use the rest variable after these operations to get the rest of the object that is not contained in forbiddenKeys. Is there a way to do this so that it works like the "Rest in object destructuring" section here? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Computed_object_property_names_and_destructuring

Marc Sloth Eastman
  • 693
  • 1
  • 10
  • 19
  • Do you have a fixed amount of computed property names or are there always two of them? – Bergi Feb 13 '20 at 22:43
  • You really should just copy them manually: `const rest = {}; for (const p in a) if (!forbiddenKeys.includes(p)) rest[p] = a[p];` – Bergi Feb 13 '20 at 22:44
  • @Bergi it is a dynamic amount, not always two. Yes, I figured out a way to do it without destructuring – Marc Sloth Eastman Feb 14 '20 at 04:29

0 Answers0