[英]How to create dynamic variable name based on value of objects property
How to create dynamic variable name based on value of objects property.如何根据对象属性的值创建动态变量名称。 for eg:例如:
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.基本上我想要“imgdata.serverid”值即。 "6" = imgdata
. "6" = imgdata
。
Can anybody help me get this right?谁能帮我解决这个问题?
Thanks谢谢
"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. “6”不是有效变量,但如果您选择一个可以作为有效变量的值,则可以完成此操作,但仅限于 widnow。 I repeat "using window object only" otherwise it's not possible in Javascript to create dynamic variable.我重复“仅使用 window object”,否则无法在 Javascript 中创建动态变量。
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
. hello
是imgdata.serverid
的值,这里hello
将成为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
.这是一个 JS 特性,我们可以在没有 window 的情况下调用任何window object's property
,例如window.console
可以称为console
。
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. 6 或“6”的变量名不符合这些规则。
Additionally, 6 or "6" could easily be misinterpretted.此外,6 或“6”很容易被误解。
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?你认为 a 和 b 会有什么值? 6 and "7", of course NOT imgdata and imgdata2. 6 和“7”,当然不是 imgdata 和 imgdata2。
And, how would javascript interpret:并且,javascript 将如何解释:
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.例如,您不能使用计数器或交互变量,因为一旦它们达到 6,下一个数字就会是 8,因为 6=7 且 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.因此,123 和“123”可能看起来相同,但它们是不同类型的对象——一个数字和一个字符串——两者都是完全有效的。 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.然而,abc 和 "abc" 也可能看起来相同,但 abc 没有任何意义,除非它之前被定义为一个变量,而 "abc" 是一个字符串 - 因此,如果 abc 没有被定义, let x = abc 将触发错误定义为变量,但 let x = "abc" 是完全有效的。
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).您将需要使用另一种方法来存储您的值或使用 window["xxx" + imgdata.serverid] 格式的有效变量名称(如果您仍然需要动态变量名称并将“xxx”替换为您选择的前缀). My suggestion is to use a map - the key being imgdata.serverid and its value being imgdata.我的建议是使用 map - 键是 imgdata.serverid,它的值是 imgdata。 That way, you can always retrieve the value using mymap.get(6);这样,您始终可以使用 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. Map 键可以是字符串或数字,您可以在同一个 map 中使用这两者 - 您只需要记住 mymap.get(6) 和 mymap.get("6") 将指向不同的 map 条目。 And, you can change the value at any time using mymap.set(6, newvalue).而且,您可以随时使用 mymap.set(6, newvalue) 更改该值。
This will give serverid = 6
.这将给出serverid = 6
。
imgdata
is object
so you need to use object
for destructuring imgdata
是object
所以你需要使用object
解构
let imgdata = {
serverid: 6,
name: 'test.jpg'
}
let {serverid} = imgdata;
imgdata.serverid
should be the expression on the right hand side. imgdata.serverid
应该是右侧的表达式。
let serverid = imgdata.serverid;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.