Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor blaze select a specific item by the array index [duplicate]

Is there a way to access array index inside each block helper in meteor blaze?

I am looking for something like this.

{{#each myarray}}
    {{this.arrayIndex3}}
{{/each}}
like image 632
james Avatar asked Nov 10 '22 00:11

james


1 Answers

I'm afraid there is not yet a standard way to do this, however you can write a helper that maps your array to a list of index / value pairs and iterate over it to display what you want.

JS

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

HTML

<template name="myTemplate">
  {{#each myArrayWithIndex}}
    myArray[{{index}}] == {{value}}
  {{/each}}
</template>

You could also define your own block helper called {{#eachWithIndex}} that would automate this process.

like image 78
saimeunt Avatar answered Nov 27 '22 02:11

saimeunt