简体   繁体   中英

Handlebars array length -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

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'}}

如果您使用的是Ember.js,则{{currentRevision.computedRoutingNodes.lastObject.numberOfReviewDays}}可以使用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM