23

Looking for a way to access to achieve this:

{{#each someArray}}
  {{../otherObject.[this]}}
{{/each}}

How do I evaluate the value of this and then reference it as a key to my object otherObject?

4
  • 4
    I have, searched for 2 days. Maybe I'm just awful at googling. Commented Oct 30, 2013 at 2:44
  • 1
    This one from the sidebar is about 90% of the solution. Basically, you have to write our own helper that converts things like {{value_at obj key}} to the JavaScript obj[key]. Commented Oct 30, 2013 at 3:07
  • 3
    I've seen that answer, the second one refers to what I'm talking about, and I was hoping for a similar solution because it seems like such an obvious use case. It's hard to believe that handlebars doesn't support this syntax... Commented Oct 31, 2013 at 6:34
  • Did you find any workaround @KevinWang? I'm stuck on the same issue... Commented Sep 25, 2014 at 16:08

2 Answers 2

18

With lookup: https://handlebarsjs.com/guide/builtin-helpers.html#lookup

{{#each someArray}}
  {{lookup ../otherObject this}}
{{/each}}
Sign up to request clarification or add additional context in comments.

2 Comments

best answer that is
this is printing [object Object]
5

One possible solution with a helper:

/*
{{#each someArrayOfKeys}}
  {{#withItem ../otherObject key=this}}
    {{this}}
  {{/withItem}}
{{/each}}
*/

Handlebars.registerHelper('withItem', function(object, options) {
    return options.fn(object[options.hash.key]);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.