Javascript 如何通过车把中的索引访问访问数组项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8044219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access an access array item by index in handlebars?
提问by lukemh
I am trying to specify the index of an item in an array within a handlebars template:
我正在尝试在车把模板中指定数组中项目的索引:
{
people: [
{"name":"Yehuda Katz"},
{"name":"Luke"},
{"name":"Naomi"}
]
}
using this:
使用这个:
<ul id="luke_should_be_here">
{{people[1].name}}
</ul>
If the above is not possible, how would I write a helper that could access a spefic item within the array?
如果以上是不可能的,我将如何编写一个可以访问数组中特定项目的帮助程序?
回答by dhorrigan
Try this:
尝试这个:
<ul id="luke_should_be_here">
{{people.1.name}}
</ul>
回答by Arjan
The following, with an additional dot before the index, works just as expected. Here, the square brackets are optional when the index is followed by another property:
以下内容,在 index 之前有一个额外的点,按预期工作。此处,当索引后跟另一个属性时,方括号是可选的:
{{people.[1].name}}
{{people.1.name}}
However, the square brackets are requiredin:
但是,在以下情况下需要方括号:
{{#with people.[1]}}
{{name}}
{{/with}}
In the latter, using the index number without the square brackets would get one:
在后者中,使用不带方括号的索引号将得到一个:
Error: Parse error on line ...:
... {{#with people.1}}
-----------------------^
Expecting 'ID', got 'INTEGER'
As an aside: the brackets are (also) used for segment-literal syntax, to refer to actual identifiers (not index numbers) that would otherwise be invalid. More details in What is a valid identifier?
顺便说一句:括号(也)用于段文字语法,以引用否则无效的实际标识符(不是索引号)。在更多细节什么是合法的标识符?
(Tested with Handlebars in YUI.)
(在 YUI 中使用 Handlebars 进行测试。)
2.xx Update
2.xx 更新
You can now use the get
helper for this:
您现在可以get
为此使用帮助程序:
(get people index)
although if you get an error about index needing to be a string, do:
尽管如果您收到有关索引需要为字符串的错误,请执行以下操作:
(get people (concat index ""))
回答by FMQB
{{#each array}}
{{@index}}
{{/each}}
回答by Bret Weinraub
If undocumented features aren't your game, the same can be accomplished here:
如果未记录的功能不是你的游戏,同样可以在这里完成:
Handlebars.registerHelper('index_of', function(context,ndx) {
return context[ndx];
});
Then in a template
然后在模板中
{{#index_of this 1}}{{/index_of}}
I wrote the above before I got a hold of
我在掌握之前写了上面的内容
this.[0]
I can't see one getting too far with handlebars without writing your own helpers.
如果不编写自己的助手,我看不到一个人在车把上走得太远。
回答by julesbou
If you want to use dynamic variables
如果要使用动态变量
This won't work:
这行不通:
{{#each obj[key]}}
...
{{/each}}
You need to do:
你需要做:
{{#each (lookup obj key)}}
...
{{/each}}
回答by user1378423
Please try this, if you want to fetch first/last.
如果您想首先/最后获取,请尝试此操作。
{{#each list}}
{{#if @first}}
<div class="active">
{{else}}
<div>
{{/if}}
{{/each}}
{{#each list}}
{{#if @last}}
<div class="last-element">
{{else}}
<div>
{{/if}}
{{/each}}
回答by Fatih Acet
While you are looping in an array with each
and if you want to access another array in the context of the current item you do it like this.
当你在一个数组中循环时each
,如果你想在当前项目的上下文中访问另一个数组,你可以这样做。
Here is the example data.
这是示例数据。
[ { name: 'foo', attr: [ 'boo', 'zoo' ] }, { name: 'bar', attr: [ 'far', 'zar' ] } ]
Here is the handlebars to get the first item in attr
array.
这是获取attr
数组中第一个项目的把手。
{{#each player}} <p> {{this.name}} </p> {{#with this.attr}} <p> {{this.[0]}} </p> {{/with}} {{/each}}
This will output
这将输出
<p> foo </p> <p> boo </p> <p> bar </p> <p> far </p>
回答by José
The following syntax can also be used if the array is not named (just the array is passed to the template):
如果数组未命名(仅将数组传递给模板),也可以使用以下语法:
<ul id="luke_should_be_here">
{{this.1.name}}
</ul>
回答by Sharukh Mastan
In my case I wanted to access an array inside a custom helper like so,
在我的情况下,我想像这样访问自定义助手中的数组,
{{#ifCond arr.[@index] "foo" }}
Which did not work, but the answer suggested by @julesbou worked.
哪个不起作用,但@julesbou 建议的答案起作用了。
Working code:
工作代码:
{{#ifCond (lookup arr @index) "" }}
Hope this helps! Cheers.
希望这可以帮助!干杯。