Return array item by index/variable in a meteor spacebars template

雨燕双飞 提交于 2019-12-25 08:30:24

问题


How can I access the X item of an array inside a meteor template?

Thanks to Return array item by index in a meteor spacebars template I know how to do that for a specific index:

<p>{{array.[0]}}</p>

But my question is how to do that for a runtime defined index. Let say that X is defined and with a valid value. Why is this not working for me?

<p>{{array.[X]}}</p>

回答1:


You can try

<p>{{array.[index]}}</p> eg. <p>{{array.[0]}}</p>

or

{{#each getArray}}
    <div class="item" data-value="{{someHelper @index}}">
        {{this}}
    </div>
{{/each}}



回答2:


Same problem here,

I end using a general helpers, {{arrayIndex array index}}

As simple as

import { Template } from 'meteor/templating'

Template.registerHelper('arrayIndex', function (array, index) {
  return array[index]
})


来源:https://stackoverflow.com/questions/41815019/return-array-item-by-index-variable-in-a-meteor-spacebars-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!