1

is there a way to get an index of an array within a handlebar template and I want to get the last value in the array and call a . property on it

{{currentRevision.computedRoutingNodes.length-1.numberOfReviewDays}}

computedRoutingNodes is an array of objects

I know i can get an index like

{{currentRevision.computedRoutingNodes.1.numberOfReviewDays}}

but I want to get the last value dynamically

blackmind
  • 1,286
  • 14
  • 27
  • possible duplicate of [Getting the last element from a JSON array in a Handlebars template](http://stackoverflow.com/questions/10755092/getting-the-last-element-from-a-json-array-in-a-handlebars-template) – McGarnagle Oct 31 '14 at 16:17
  • @McGarnagle I think this is a somehow different operation since the op is looking to get a property of the last item and not the last item itself. It seems like you should be able to adapt the code that you posted though... – k-nut Oct 31 '14 at 16:18

2 Answers2

1

You could use a helper:

Handlebars.registerHelper('propAtLengthRelativeIndex', function (arr, index, prop) {
    return new Handlebars.SafeString(arr[arr.length + ~~index][prop]);
});

And then call it like this:

{{propAtLengthRelativeIndex currentRevision.computedRoutingNodes '-1' 'numberOfReviewDays'}}
Talmid
  • 1,273
  • 14
  • 19
1

{{currentRevision.computedRoutingNodes.lastObject.numberOfReviewDays}} will work if you're using Ember.js.

undeletable
  • 1,375
  • 12
  • 11