简体   繁体   English

如何获得变量的值?

[英]How do I get get the value of a variable?

This is my a snippet code from my nodeJs progam here. 这是我的nodeJs程序的摘录代码。 I have a map called transMap. 我有一张叫做transMap的地图。 I would like to read the value for yy, get "ipaddress" and pass it out as json along with its value. 我想读取yy的值,获取“ ipaddress”,并将其连同其值一起作为json传递出去。 I would have expected json response to be { "ipaddress" : "http://derivedURL.com"} . 我希望json响应为{ "ipaddress" : "http://derivedURL.com"} But what I get is { arg : "http://derivedURL.com"} 但是我得到的是{ arg : "http://derivedURL.com"}

I am missing something really basic here. 我在这里错过了一些非常基本的东西。

var transMap = new Map();
        transMap.set('xx', {rPath:'/xxs', sn:2,res:['rid']});
        transMap.set('yy', {rPath:'/yys', sn:3, res: ['ipaddress','dns']});
        transMap.set('zz', {rPath:'/zzs', sn:4, res:['uri', 'fqdn']});

       var arg = (transMap.get(yy)).res[0] ; //arg = ipaddress
       var jsonResponse = { arg : "http://derivedURL.com"}; 
       console.log(jsonResponse); //

Edit1: arg has to get the value of "ipaddress" obtained from transMap. Edit1:arg必须获取从transMap获得的“ ipaddress”的值。 Derived URL is derived from a different function thats immaterial to this discussion. 派生的URL是从与本讨论无关紧要的不同函数派生的。

My console now reads: { arg : "http://derivedURL.com"} . 我的控制台现在显示为: { arg : "http://derivedURL.com"} But I want it to read { "ipaddress" : "http://derivedURL.com"} 但我希望它阅读{ "ipaddress" : "http://derivedURL.com"}

When you do : 当您这样做时:

var jsonResponse = {
  arg : 'someArg'
}

arg does not get evaluated to it's value. arg的值未得到评估。 Instead it gets used as 'arg' itself. 相反,它本身被用作“ arg”。 To have it evaluated to it's value, you can set it like this: 要对其值求值,可以这样设置:

jsonResponse[arg] = 'someArg'

When you use the [] notation, the key (here: arg ) always gets evaluated in scope. 当使用[]表示法时,键(此处为arg )始终在范围内进行求值。

Example jsFiddle jsFiddle示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM