简体   繁体   English

Handlebars.js - 使用变量键访问对象值

[英]Handlebars.js - Access object value with a variable key

Looking for a way to access to achieve this: 寻找一种方法来实现这一点:

{{#each someArray}}
  {{../otherObject.[this]}}
{{/each}}

How do I evaluate the value of this and then reference it as a key to my object otherObject ? 如何评估值this ,然后引用它作为重点,以我的对象otherObject

With lookup: http://handlebarsjs.com/builtin_helpers.html#lookup 使用查找: http//handlebarsjs.com/builtin_helpers.html#lookup

{{#each someArray}}
  {{lookup ../otherObject this}}
{{/each}}

One possible solution with a helper: 一个可能的帮助解决方案:

/*
{{#each someArrayOfKeys}}
  {{#withItem ../otherObject key=this}}
    {{this}}
  {{/withItem}}
{{/each}}
*/

Handlebars.registerHelper('withItem', function(object, options) {
    return options.fn(object[options.hash.key]);
});

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

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