[英]how to access first key from a map having list of values as object in coffee script
I am accessing this yaml file (converted to json) using a rest call. 我正在使用rest调用访问此yaml文件(转换为json)。
A:
B:
C: [ value1, value2, value3 ]
D: [ value4, value5, value6 ]
looking at the json object on the uri, I see it being displayed like 看着uri上的json对象,我看到它像
{"C":["value1","value2","value3"],"D":["value4","value5","value6"]}
This result is expected, the way I am traversing the yaml file and sending it. 这是预期的结果,这是我遍历yaml文件并将其发送的方式。
However, when I try to access the first key "C" of the map (yamlmap) using coffee script: 但是,当我尝试使用coffee脚本访问地图(yamlmap)的第一个键“ C”时:
alphabet= (key for key,value of yamlmap)
It is not displaying anything. 它没有显示任何内容。 Is this the right way ?
这是正确的方法吗?
As @mu-is-too-short have commented, your code is 'almost' right. 正如@ mu-is-too-short所说,您的代码“几乎”正确。 But I'd recommend below.
但我建议在下面。
alphabet = Object.keys yamlmap
Furthermore explanation would be easily understandable by trying web compiler on http://coffeescript.org and comparing result of it. 通过在http://coffeescript.org上尝试使用Web编译器并比较其结果,可以很容易地理解进一步的解释。
In short, 简而言之,
alphabet = (key for key of yamlmap)
which would be better than your code (key for key,value of yamlmap)
which is getting-and-ignoring value of the object, is still going to be equivalent in js this big below 这将比您的代码
(key for key,value of yamlmap)
要好得多,而该代码却越来越忽略对象的值,在下面的js中仍然等效
alphabet = (function(){
var results = [];
for (key in yamlmap)
results.push(key)
return results;
})();
While the code I recommend would be almost the same in js like below and about 2 to 3 times faster than above. 虽然我建议的代码在js中与以下代码几乎相同,但比上面的代码快2至3倍。
alphabet = Object.keys(yamlmap);
Many template languages can have this kind of problem and we should be aware on that. 许多模板语言都可能存在此类问题,我们应该意识到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.