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?
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 comment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With