Jafar Rasooli Jafar Rasooli - 1 month ago 9
Javascript Question

how can I reversely iterate on an aurelia array repeater?

I am trying to iterate from the end of an array to the beginning of it.

For example:

repeater-template.js:

export class RepeaterTemplate {
constructor() {
this.friends = [
'Alice',
'Bob',
'Carol',
'Dana'
];
}
}


repeater-template.html:

<template>
<p repeat.for.reverse ="friend of friends">Hello, ${friend}!</p>
</template>


output:

Hello Dana
Hello Carol
Hello Bob
Hello Alice

Answer Source

As far as I know Aurelia doesn't have any explicit functions for this, but you could make a simple function in the viewmodel:

reverse(arr) {
    return arr.slice().reverse();
}

Then in the view:

<template>
   <p repeat.for="friend of reverse(friends)">Hello, ${friend}!</p>
</template>