Meteor blaze按数组索引选择特定项

jam*_*mes 5 meteor meteor-blaze

有没有办法在流星大火中的每个块助手中访问数组索引?

我正在寻找这样的东西.

{{#each myarray}}
    {{this.arrayIndex3}}
{{/each}}
Run Code Online (Sandbox Code Playgroud)

sai*_*unt 1

恐怕还没有一种标准方法可以做到这一点,但是您可以编写一个帮助器,将数组映射到索引/值对列表,并对其进行迭代以显示您想要的内容。

JS

Template.myTemplate.helpers({
  myArrayWithIndex: function(){
    return _.map(this.myArray,function(value,index){
      return {
        index:index,
        value:value
      };
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

超文本标记语言

<template name="myTemplate">
  {{#each myArrayWithIndex}}
    myArray[{{index}}] == {{value}}
  {{/each}}
</template>
Run Code Online (Sandbox Code Playgroud)

您还可以定义自己的块帮助器,{{#eachWithIndex}}该帮助器将自动执行此过程。