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

  • <tfoot id='sofx8'></tfoot>
    • <bdo id='sofx8'></bdo><ul id='sofx8'></ul>

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

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

      1. 使用把手迭代 javascript 对象

        时间:2024-04-19

            <tbody id='JsPWv'></tbody>
          • <tfoot id='JsPWv'></tfoot>

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

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

                  本文介绍了使用把手迭代 javascript 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Handlebars 注册帮助程序以允许迭代 JSON 对象.这个要点 看起来是一个合适的解决方案.我将其转换为以下 CoffeeScript.当我使用任何一个帮助程序时,似乎什么都没有发生(对于 vanilla JavaScript 和 CoffeeScript 版本都是如此).有什么想法吗?

                  I am trying to register helpers with Handlebars to allow iterating over JSON objects. This gist looked like an appropriate solution. I converted that into the following CoffeeScript. Nothing seems to happen when I use either of the helpers (that holds true for both vanilla JavaScript and the CoffeeScript version). Any ideas?

                  $ ->
                    Handlebars.registerHelper "key_value", (obj, fn)->
                      buffer = ""
                      key 
                      for key in obj 
                        if obj.hasOwnProperty(key)
                          buffer += fn({key: key, value: obj[key]})
                      buffer
                  
                    Handlebars.registerHelper "each_with_key", (obj, fn)->
                      context
                      buffer = ""
                      key 
                      keyName = fn.hash.key
                      for key in obj 
                        if obj.hasOwnProperty(key)
                          context = obj[key]
                          if keyName
                            context[keyName] = key 
                            buffer += fn(context)
                      buffer
                  

                  在模板中:

                  {{#key_value categories}}
                  I'M ALIVE!!
                  {{/key_value}}
                  
                  {{#each_with_key categories key="category_id"}}
                  I'M ALIVE!!
                  {{/each_with_key}}
                  

                  我目前在 Gemfile 中使用 gem 'handlebars-assets' 将把手添加到 Rails 应用程序.

                  I am currently using gem 'handlebars-assets' in the Gemfile to add handlebars to a rails app.

                  推荐答案

                  你的 JavaScript 到 CoffeeScript 的音译坏了.你不使用 for ... in 来迭代 CoffeeScript 中的对象,你使用 for k,v of ...:

                  Your JavaScript to CoffeeScript transliteration is broken. You don't use for ... in to iterate over an object in CoffeeScript, you use for k, v of ...:

                  使用 of 表示理解对象的属性而不是数组中的值.

                  Use of to signal comprehension over the properties of an object instead of the values in an array.

                  这个 CoffeeScript 循环:

                  This CoffeeScript loop:

                  for x in y
                      ...
                  

                  变成这个 JavaScript:

                  becomes this JavaScript:

                  for (_i = 0, _len = y.length; _i < _len; _i++) {
                    x = a[_i];
                    ...
                  }
                  

                  所以如果 y 是一个没有 length 属性的对象,那么 _len 将是 undefined 并且 JavaScriptfor(;;) 循环根本不会迭代.

                  So if y is an object without a length property, then _len will be undefined and the JavaScript for(;;) loop won't iterate at all.

                  您还应该使用 own 而不是 hasOwnProperty:

                  You should also be using own instead of hasOwnProperty:

                  如果您只想迭代对象本身上定义的键,通过添加 hasOwnProperty 检查以避免可能从原型中插入的属性,请使用 for own对象的键、值.

                  If you would like to iterate over just the keys that are defined on the object itself, by adding a hasOwnProperty check to avoid properties that may be interited from the prototype, use for own key, value of object.

                  但这更多是为了方便而不是正确.

                  but that's more for convenience than correctness.

                  此外,CoffeeScript 循环是表达式,因此您通常会说 array = expr for own k, v in o 或等效形式:

                  Also, CoffeeScript loops are expressions so you'd usually say array = expr for own k, v in o or the equivalent form:

                  array = for own k, v in o
                      expr
                  

                  如果 expr 超过一行或太长而无法理解.

                  if expr is more than one line or too long to allow for a readable comprehension.

                  CoffeeScript 中帮助程序的正确且更惯用的版本看起来更像这样:

                  A correct and more idiomatic version of your helpers in CoffeeScript would look more like this:

                  Handlebars.registerHelper "key_value", (obj, fn)->
                      (fn(key: key, value: value) for own key, value of obj).join('')
                  
                  Handlebars.registerHelper "each_with_key", (obj, fn)->
                      key_name = fn.hash.key
                      buffer   = for own key, value of obj
                          value[key_name] = key
                          fn(value)
                      buffer.join('')
                  

                  演示:http://jsfiddle.net/ambiguous/LWTPv/

                  这篇关于使用把手迭代 javascript 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:CoffeeScript - 无与伦比的 OUTDENT 下一篇:Azure App 服务无法与 React-Redux Web 应用程序中的自定义路由一起使用 - 需要通配符虚拟目录吗

                  相关文章

                • <tfoot id='NhgNT'></tfoot>

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

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

                        <bdo id='NhgNT'></bdo><ul id='NhgNT'></ul>