最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I reverse the ordering of spacebars #each block - Stack Overflow

programmeradmin0浏览0评论

A helper sentencesList is an array of objects containing an element text.

HTML:

{{#each sentencesList}}
  {{text}}
{{/each}}

CLIENT.JS

sentencesList: function() {
    return Session.get(SENTENCES)
}

How can I reverse the ordering, i.e the highest index number is shows at the top and the element at index position 0 is at the bottom?

A helper sentencesList is an array of objects containing an element text.

HTML:

{{#each sentencesList}}
  {{text}}
{{/each}}

CLIENT.JS

sentencesList: function() {
    return Session.get(SENTENCES)
}

How can I reverse the ordering, i.e the highest index number is shows at the top and the element at index position 0 is at the bottom?

Share Improve this question asked May 11, 2015 at 14:44 meteorBuzzmeteorBuzz 3,2005 gold badges36 silver badges62 bronze badges 1
  • Assuming it's an array developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Brian Shamblen Commented May 11, 2015 at 14:49
Add a ment  | 

2 Answers 2

Reset to default 8

You could just reverse the array:

sentencesList: function() {
    return Session.get(SENTENCES).reverse();
}

Please note: although {{#each}} works on both arrays and cursors, this method is array-specific. So you cannot use it, say, on the return of a Collection.find() call. To achieve this with a collection, you will have to use sort on the key you want to reverse order from.

If you want to reverse more than one array, you could create a generic helper in (say) main.js:

Template.registerHelper('reverseArray', (array) => array.reverse());

In your template:

{{#each (reverseArray sentencesList)}}
  {{text}}
{{/each}}

EDIT: Updated code to include parentheses as per Pierre's ment

发布评论

评论列表(0)

  1. 暂无评论