Dynamic object names?

Question: Is there a way to use a variable as an object name, for example in JS using bracket notation to access to property?

I’m not sure if I am asking this correctly please see example below.

{{ $key_passed := ‘mykey’ }}

{{ range $index, $element := $.Site.Data.a_list[$key_passed] }}

{{  end }}

If not, I have found workarounds using index but just wanted to make sure. Also thanks Hugo team I really enjoy using your ssg (switched from Jekyll)

Actually figured it out. Using the index function.

Example below.

{{ $key_passed := 'mykey' }}

{{ range $index, $element := (index $.Site.Data.a_list $key_passed) }}

{{  end }}
1 Like