0

If I had a large object, and needed all the properties of that object, is there a quick way to destructure that object?

So this:

const foo = {
  name1: 'foo',
  num1: 5,
  bool1: true
  friends1: ['bar', 'fizz', 'buzz'],
  name2: 'foo',
  num2: 5,
  bool2: true
  friends2: ['bar', 'fizz', 'buzz'],
  name3: 'foo',
  num3: 5,
  bool3: true
  friends3: ['bar', 'fizz', 'buzz'],
}

const {name1, num1, bool1, friends1, name2, num2, bool2, friends2, name3, num3, bool3, friends3} = foo;

Becomes this:

const {*} = foo;
Bagel03
  • 725
  • 7
  • 22
  • No, there's no such shortcut. – Barmar Nov 16 '20 at 20:52
  • What is the purpose of doing so if you don't have a representative for the deconstructed value? what is the goal? – Shimi Nov 16 '20 at 20:53
  • @Barmar Ok, was just making sure – Bagel03 Nov 16 '20 at 20:54
  • 1
    This looks like an object that would have a dynamic number of properties, but destructuring happens at compile time, not run time. – Barmar Nov 16 '20 at 20:54
  • 3
    Does this answer your question? [How do I destructure all properties into the current scope/closure in ES2015?](https://stackoverflow.com/questions/31907970/how-do-i-destructure-all-properties-into-the-current-scope-closure-in-es2015) – Nathan Chu Nov 16 '20 at 20:57
  • maybe `const { ...properties } = foo;` but it actually make no sense – bill.gates Nov 16 '20 at 21:25

0 Answers0