0

Say I am defining a series of variables like so:

let test = {one:1, two:2, three:3}
let {one, two, three} = test

This would define the variables one, two, and three with the corresponding values in the test variable. Now say I want to define all of the values in test as variables but I don't know the elements inside it. How could I do that?

So far I've tried using an array in it like

let {["one", "two", "three"]} = test

but I get errors

let {["one", "two", "three"]} = test
           ^

Uncaught SyntaxError: Unexpected token ','

Then I tried using the spread operator first like

let {...["one", "two", "three"]} = test

But that didn't work either and had the same error.

I have absolutely no idea where to continue from here.

  • 2
    You cannot define variables with dynamic names; the concept doesn't really make sense. The definition of a variable with `var`, `let`, or `const` is the binding of a name into the relevant scope. If you don't know the name, there's nothing to bind, and there would be no way to make use of the variable anyway in code in that scope. – Pointy Apr 28 '21 at 14:13
  • @Pointy I understand how this can be a niche use case but for me I am attempting to load all of the variables from a `require()` statement into variables in my current scope but since I update the keys in the json frequently, I don't want to also have to update the script each time. Am I looking at this use case wrong and is there a different solution? – Nationalism Apr 28 '21 at 14:16
  • Thank you for the close! I couldn't find this anywhere! – Nationalism Apr 28 '21 at 14:18
  • How would you make use of any of the variables in subsequent code? – Pointy Apr 28 '21 at 14:18
  • as you would with any variable. That question to my understanding seems very vague, I would use them as objects to store information? – Nationalism Apr 28 '21 at 14:19
  • @Asian I don't think that something like this will solve your problem, because even if you manage to put all the keys from json in js variables dynamically, you will have to use them in the code by name - so you really don't achieve anything by doing this. – Noy Gafni Apr 28 '21 at 14:46
  • @NoyGafni yes, I will be using the variables in the script but this is more for convinience's sake – Nationalism Apr 28 '21 at 14:49
  • You are not understanding the question. If you have a block of code with variable names that you don't know in advance, how would you (for example) write a statement to add two of the variables together and store the result in a third variable? – Pointy Apr 28 '21 at 14:54
  • 1
    @Pointy exactly, what I am saying is that if you use the "dynamic" variables by name in the code it self - you will need to change all the usages of them when you change your keys in the json. so to define them dynamically will not help you at all because you need to change the code no matter what – Noy Gafni Apr 28 '21 at 15:05
  • @Pointy Going through the duplicated post, I see people talking about using `eval`, I am not doing any user-input or front-end work so could you potentially direct me to an example of using `eval`? Sorry for taking your time up – Nationalism Apr 28 '21 at 18:54
  • @Nationalism there are almost certainly better ways of solving your implementation issues than using `eval()`. – Pointy Apr 28 '21 at 18:57

0 Answers0