LeeR
LeeR

Reputation: 1636

HandlebarJS Using result of another property as name for somewhere else

So, I'm trying to get my head around handlebar JS and I'm running in to some unknowns for me.

Say I have the following JS object.

{{"labels": ["arms", "leg", "chest"], "arms-avg": 33, "leg-avg": 13, "chest-avg": 55}}

I want to do something like the following, but not sure how

{{#each labels}}
    <div>{{this}} + '-avg'</div>
{{/each}}

Hopefully that makes sense, but please let me know if you I haven't been too clear

Thanks in advance

Upvotes: 0

Views: 26

Answers (1)

Jairo Malanay
Jairo Malanay

Reputation: 1347

{{#each labels as |label|}}
    <div>{{this}}-avg  => {{testHelper this}}
{{/each}}

Handlebars.registerHelper('testHelper', function(key, options) {
    return options.data.root[key +'-avg'];
});

will return the value of the key

<div>arms-avg  => 33
<div>leg-avg  => 13
<div>chest-avg  => 55

Upvotes: 1

Related Questions