0

Is it possible to get a property of an object having the key being another property?

Handlebars data:

{
   person: {
      firstName: 'John',
      lastName: 'Doe'
   },
   propertyToGetFromPerson: 'firstName'
}

Then in the template itself I would like to get firstName of person but by using the propertyToGetFromPerson property in stead of the literal 'firstName'.

In JavaScript this would be: person[propertyToGetFromPerson]

So I am searching for the equivalent in handlebars if there is one.

I am looking for an answer without using helpers, if there is no answer I can write a helper.

Laurent Dhont
  • 1,012
  • 1
  • 9
  • 22
  • 1
    You must use the `lookup` built-in helper. See: https://handlebarsjs.com/guide/builtin-helpers.html#lookup. Also: https://stackoverflow.com/a/54274111/3397771 – 76484 Jun 13 '20 at 18:47
  • @76484 Thanks a lot, it worked! I read this before on the docs but I interpreted it wrong. If you post this as an answer I can accept ;-) – Laurent Dhont Jun 15 '20 at 05:35
  • I think this question, or variations on it, has been answered too many times already. In the interest of avoiding bloat on this site, I think it would be better if you found one of the existing answers and up-voted it. – 76484 Jun 20 '20 at 23:17

0 Answers0