简体   繁体   English

如何从以咖啡脚本中的值列表作为对象的地图访问第一个键

[英]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.

相关问题 如何在具有json字符串的java脚本对象中访问特定的键值对 - How to access particular Key-Value pair in java script object having a json string 如何迭代一个列表<Map<String,String> &gt; 并将其键值添加到 Java 对象中 - How to iterate a List<Map<String,String>> and add its key,values in a Java Object 如何访问嵌套的JSON对象键和值 - How to access nested JSON object key and values RestKit对象映射:如何从键值映射到新对象/关系? - RestKit Object Mapping: How to map from key values to new object / relation? 如何从序列化为JSON的java.util.Map中访问值,其中键是带有Javascript的ebean.Model - How to access values from a java.util.Map that was serialized to JSON where the key is an ebean.Model with Javascript 如何将javascript对象(从json webmethod转换)映射到其中js对象仅具有1个属性和多个值的数组,使用JQuery - how to map javascript object(converted from json webmethod) into array where js object having 1 property only and multiple values USING JQuery 当用户从 Vue.js 中的 JSON 对象中选择第一个键时,如何显示剩余的键值? - How can I display the remaining key values when a user selects the first key from a JSON object in Vue.js? 获取后无法从 Reactjs 中的 Object 访问键值 - Cant access key values from an Object in Reactjs after fetching jsonpath:当键名称中包含“连字符”时,如何访问当前对象中的键? - jsonpath : How to access a key in current object when the key name is having “hyphen” in it? 列表中的Json值 <Map<String, Object> &gt;在javascript emberjs中 - Json Values from List<Map<String, Object>> in javascript emberjs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM