0

How to create dynamic variable name based on value of objects property. for e.g:

let imgdata = {
   serverid: 6,
   name: 'test.jpg'
}

//this does not work 
let [imgdata.serverid] = imgdata

//Obviously this won't work
myArr.push([imgdata.serverid])

Basically I want the "imgdata.serverid" value ie. "6" = imgdata.

Can anybody help me get this right?

Thanks

appu
  • 471
  • 1
  • 8
  • 26

4 Answers4

1

"6" is not a valid variable but if you choose a value that can be a valid variable then this can be done but on widnow only. I repeat "using window object only" otherwise it's not possible in Javascript to create dynamic variable.

let imgdata = {
   serverid: 'hello',
   name: 'test'
};

window[imgdata.serverid] = imgdata;
console.log(hello);

hello is the value of imgdata.serverid, here hello will become property of window object. And it's a JS feature that we can call any window object's property without window, like window.console can be called as console.

intekhab
  • 1,566
  • 1
  • 13
  • 19
1

The rules for variable names are:

1 - They must be unique to the scope
2 - They must start with a letter, $ or _
3 - They are case-sensitive - so, y and Y are two different variables
4 - They must not be keywords/reserved words - such as "if", "return", "class" etc

A variable name of either 6 or "6" does not meet those rules.

Additionally, 6 or "6" could easily be misinterpretted.

For example, if we assume that the following runs without errors (which it won't, of course):

let 6 = imgdata;
let "7" = imgdata2;

let a = 6;
let b = "7";

What values do you think a and b will have? 6 and "7", of course NOT imgdata and imgdata2.

And, how would javascript interpret:

let 6 = 7;
let "a" = "b";

? And, if you could do that, how would maths work from then on? You could not use counters or interator variables, for example, because, as soon as they got to 6, the next number would be 8 because 6=7 and 7+1 = 8.

The point here is that letters have meaning within a string but not outside of it, so can be safely used for variable names (within the rules above). Numbers, however, can be either part of a string OR numerics. Thus, 123 and "123" may look the same but are different types of objects - a number and a string - and both are perfectly valid. However, abc and "abc" may also look the same but abc has no meaning unless it has been previously defined as a variable, whereas "abc" is a string - thus, let x = abc will trigger an error if abc has not been defined as a variable, but let x = "abc" is perfectly valid.

You will need to use another means of storing your values or use a valid variable name using the window["xxx" + imgdata.serverid] format (if you still need a dynamic variable name and replacing "xxx" with a prefix of your choice). My suggestion is to use a map - the key being imgdata.serverid and its value being imgdata. That way, you can always retrieve the value using mymap.get(6); Map keys can be strings or numbers, and you can use both within the same map - you just have to remember that mymap.get(6) and mymap.get("6") will point to different map entries. And, you can change the value at any time using mymap.set(6, newvalue).

ATD
  • 1,344
  • 1
  • 4
  • 8
0

This will give serverid = 6.

imgdata is object so you need to use object for destructuring

let imgdata = {
   serverid: 6,
   name: 'test.jpg'
}

let {serverid} = imgdata;
aRvi
  • 2,203
  • 1
  • 14
  • 30
0

imgdata.serverid should be the expression on the right hand side.

let serverid = imgdata.serverid;
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • `let serverid = imgdata.serverid` sets `serverId = 6`, this is not what I want, the resultant expression should be `"6" = { serverid: 6, name: 'test.jpg' }` – appu Sep 21 '20 at 19:31
  • That makes no sense. A variable name can't be a number. – Barmar Sep 21 '20 at 19:33
  • And you don't put quotes around variable names. – Barmar Sep 21 '20 at 19:33
  • pls check it's not a number, it a string derived from `imgdata.serverid` – appu Sep 21 '20 at 19:34
  • A string isn't a variable, either. – Barmar Sep 21 '20 at 19:34
  • 1
    See https://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript regarding dynamic variables in JS. – Barmar Sep 21 '20 at 19:35
  • 1
    Even if, for a second, we assume that you could create a dynamic variable name of 6 or "6", how would you use it? Somewhere in the rest of the code it would need to know that there is a variable by that name, but how? There has to be something else that has the same value, or equates to the same value, to be able to even attempt to use the variable's value. Why not simply create a map object and do mymap.set(imgdata.serverid, imgdata) and then use mymap.get(xxx) - where xxx is a value determined later the code and, as long as it equates to 6, it will retrieve the imgdata object. – ATD Sep 21 '20 at 20:15
  • Exactly, that's why dynamic variables are almost always the wrong idea. – Barmar Sep 21 '20 at 20:15