在车把小胡子内通过键访问数组/对象变量 [英] Accessing array/object variables by key, within a handlebars mustache
问题描述
我似乎找不到解决方法,并且尝试了子表达式和StackOverflow上发布的其他各种答案-但它们似乎都假设我已经知道我正在使用的密钥.
I can't seem to find a way of doing this, and have tried subexpressions and various other answers posted on StackOverflow - but they all seem to assume that I know the key I'm using in advance.
我有以下对象:
{
fields: {
1: {
complete: 1,
submitted: 0
},
2: {
complete: 1,
submitted: 0
}
}
等等
在我的Handlebars模板中,我正在遍历对象数组.该数组中的每个对象都有一个 field_id
键,该键是一个与上例中的1或2匹配的数字.
In my Handlebars template, I'm looping through an array of objects. Each object in that array has a field_id
key which is a number that matches up to the 1 or 2 in the above example.
基于当前循环的field_id,我需要访问上述数组结构中的变量.我尝试过:
I need to access the variables in the above array structure, based on the current field_id being looped. I've tried:
{{fields [(field_id)].complete}}
{{fields [{{field_id}}].complete}}
{{fields.(field_id).complete}}
{{fields.({{field_id}}).complete}}
它们都不起作用.
有没有办法做到这一点?
Is there a way of doing this?
推荐答案
我不会为此创建一个自定义帮助程序.相反,我将使用现有的查找助手与
I would not create a custom helper for this. Instead, I would use the existing Lookup helper partnered with the with block helper:
{{#with (lookup fields field_id) as |field|}}
{{field.complete}}
{{/with}}
或者,您可以将查找帮助器与子表达式一起使用:
Alternatively you could use the lookup helper with a subexpression:
{{lookup (lookup fields field_id) 'complete'}}
这篇关于在车把小胡子内通过键访问数组/对象变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!