I have JSON. I want to output an array element by a variable key. How can I do it? key is a variable.
{
"key": 1,
"last_request": {
"items": [
{
"town": "London"
},
{
"town": "Paris"
},
{
"town": "Lisbon"
}
]
}
}
If I use {{last_request.items.[0].town}}, I have no problem.
But {{last_request.items.[key].town}} is not working.
Handlebars has a built-in lookup helper for the purpose of getting a value from an object using a dynamic key. In your case, because the
keyis not the final property lookup, buttownis, you would need to uselookuptwice, in a nested fashion. The inner lookup would get the item atkeyand the outer lookup would get thetown. The result would be: