0

for example i have testObject like so:

const testObject = {obj1:1, obj2:2, obj3:3}

when i want destruct all elements i had to do like this:

const {obj1, obj2, obj3} = testObject

is there any way like const {*} = testObject to destruct all element is an object? thanks.

Sahandevs
  • 1,120
  • 9
  • 25
  • 3
    Perhaps edit your question to explain what problem you're trying to solve by doing this. There may be a better solution than what you're attempting. – Jordan Running May 04 '18 at 15:15
  • 1
    [`with(testObject)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with) - please take the red warning on the MDN seriously though. – ASDFGerte May 04 '18 at 15:15
  • Doing so would be akin to a `with` statement. It's better that destructuring requires you to explicitly name all the variables you're declaring, so that your code doesn't seemingly randomly reference variables that aren't declared anywhere. – Patrick Roberts May 04 '18 at 15:16
  • https://stackoverflow.com/questions/31907970/how-do-i-destructure-all-properties-into-the-current-scope-closure-in-es2015 – ericArbour May 04 '18 at 15:16
  • @ericArbour i'm using JSX should i wrap return statement with a "with"? – Sahandevs May 04 '18 at 15:19
  • 1
    I would be wary of any attempt to create a bunch of variables in your scope without explicitly naming them. –  May 04 '18 at 15:26
  • 1
    I should probably reformulate my "please take the warning seriously" to "don't do what you want to do" and think about whether this might be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – ASDFGerte May 04 '18 at 15:28
  • @ASDFGerte i took that warning in account and my elements in object all have a suffix in their key names ( like _R_keyname ) – Sahandevs May 04 '18 at 15:39

0 Answers0