<legend id='OnCZT'><style id='OnCZT'><dir id='OnCZT'><q id='OnCZT'></q></dir></style></legend>

      <i id='OnCZT'><tr id='OnCZT'><dt id='OnCZT'><q id='OnCZT'><span id='OnCZT'><b id='OnCZT'><form id='OnCZT'><ins id='OnCZT'></ins><ul id='OnCZT'></ul><sub id='OnCZT'></sub></form><legend id='OnCZT'></legend><bdo id='OnCZT'><pre id='OnCZT'><center id='OnCZT'></center></pre></bdo></b><th id='OnCZT'></th></span></q></dt></tr></i><div id='OnCZT'><tfoot id='OnCZT'></tfoot><dl id='OnCZT'><fieldset id='OnCZT'></fieldset></dl></div>
        <bdo id='OnCZT'></bdo><ul id='OnCZT'></ul>

      1. <small id='OnCZT'></small><noframes id='OnCZT'>

      2. <tfoot id='OnCZT'></tfoot>

        如何在 CoffeeScript 中迭代对象中的键和值?

        How to iterate over the keys and values in an object in CoffeeScript?(如何在 CoffeeScript 中迭代对象中的键和值?)

        <small id='nxPRB'></small><noframes id='nxPRB'>

          <i id='nxPRB'><tr id='nxPRB'><dt id='nxPRB'><q id='nxPRB'><span id='nxPRB'><b id='nxPRB'><form id='nxPRB'><ins id='nxPRB'></ins><ul id='nxPRB'></ul><sub id='nxPRB'></sub></form><legend id='nxPRB'></legend><bdo id='nxPRB'><pre id='nxPRB'><center id='nxPRB'></center></pre></bdo></b><th id='nxPRB'></th></span></q></dt></tr></i><div id='nxPRB'><tfoot id='nxPRB'></tfoot><dl id='nxPRB'><fieldset id='nxPRB'></fieldset></dl></div>
          <legend id='nxPRB'><style id='nxPRB'><dir id='nxPRB'><q id='nxPRB'></q></dir></style></legend>
        1. <tfoot id='nxPRB'></tfoot>
            <bdo id='nxPRB'></bdo><ul id='nxPRB'></ul>

                    <tbody id='nxPRB'></tbody>
                • 本文介绍了如何在 CoffeeScript 中迭代对象中的键和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个对象(可以说是关联数组" - 也称为纯 JavaScript 对象):

                  I have an object (an "associate array" so to say - also known as a plain JavaScript object):

                  obj = {}
                  obj["Foo"] = "Bar"
                  obj["bar"] = "Foo"
                  

                  我想使用 CoffeeScript 迭代 obj,如下所示:

                  I want to iterate over obj using CoffeeScript as follows:

                  # CS
                  for elem in obj
                  

                  上面的CS代码编译成JS:

                  bu the CS code above compiles to JS:

                  // JS
                  for (i = 0, len = obj.length; i < len; i++)
                  

                  在这种情况下不合适.

                  JavaScript 方式是 for(var key in obj) 但现在我想知道:我如何在 CoffeeScript 中做到这一点?

                  The JavaScript way would be for(var key in obj) but now I'm wondering: how can I do this in CoffeeScript?

                  推荐答案

                  使用 for x,y of L.相关文档.

                  ages = {}
                  ages["jim"] = 12
                  ages["john"] = 7
                  
                  for k,v of ages
                    console.log k + " is " + v
                  

                  输出

                  jim is 12
                  john is 7
                  

                  您可能还需要考虑 Aaron Dufour 在评论中提到的变体 for own k,v of age.这会添加一个检查以排除从原型继承的属性,这在本示例中可能不是问题,但如果您是在其他东西之上构建,则可能会出现问题.

                  You may also want to consider the variant for own k,v of ages as mentioned by Aaron Dufour in the comments. This adds a check to exclude properties inherited from the prototype, which is probably not an issue in this example but may be if you are building on top of other stuff.

                  这篇关于如何在 CoffeeScript 中迭代对象中的键和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                  Rails 3.1 ajax:success handling(Rails 3.1 ajax:成功处理)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)

                • <i id='tpmqi'><tr id='tpmqi'><dt id='tpmqi'><q id='tpmqi'><span id='tpmqi'><b id='tpmqi'><form id='tpmqi'><ins id='tpmqi'></ins><ul id='tpmqi'></ul><sub id='tpmqi'></sub></form><legend id='tpmqi'></legend><bdo id='tpmqi'><pre id='tpmqi'><center id='tpmqi'></center></pre></bdo></b><th id='tpmqi'></th></span></q></dt></tr></i><div id='tpmqi'><tfoot id='tpmqi'></tfoot><dl id='tpmqi'><fieldset id='tpmqi'></fieldset></dl></div>

                      <bdo id='tpmqi'></bdo><ul id='tpmqi'></ul>
                          <tbody id='tpmqi'></tbody>

                          <tfoot id='tpmqi'></tfoot>

                            <small id='tpmqi'></small><noframes id='tpmqi'>

                            <legend id='tpmqi'><style id='tpmqi'><dir id='tpmqi'><q id='tpmqi'></q></dir></style></legend>