escapeVelocity
escapeVelocity

Reputation: 166

Ember.js each loop : compare current index data with previous index data

In ember.js each loop, How can I access the data of the previous index inside the current loop?

{{#each coonfigData key="name" as |band index|}}

  {{#if band.min >  band(index-1).min}} //compare current min with previous min

     <div>Something</div>

  {{/if}}

{{/each}}

I know I can create a helper to store the previous index data inside the template scope, but is there any template only way? If not, what will be the most elegant way using a helper?

Upvotes: 1

Views: 549

Answers (1)

Ahmet Emre Kilinc
Ahmet Emre Kilinc

Reputation: 6915

You can install ember-math-helpers and use sub helper for subtract operation like and you can install ember-truth-helpers and use gt helper for greater than condition and finally built-in get helper for retrieving the previous element from the list like this:

{{#each configData key="name" as |band index|}}

  {{#if (gt band.min (get (get configData (sub index 1)) min))}} //compare current min with previous min

     <div>Something</div>

  {{/if}}

{{/each}}

Upvotes: 2

Related Questions