How to access an array at an index

Is there a way to access an array at a certain index, to retrieve that value.

i.e. something along the lines of

$array[4]

with 4 being an $index variable within a range loop.

Thanks in advance

1 Like
{{ index $array 4 }}
3 Likes

btw, check out Go documentation:
http://golang.org/pkg/text/template/#hdr-Functions

index
	Returns the result of indexing its first argument by the
	following arguments. Thus "index x 1 2 3" is, in Go syntax,
	x[1][2][3]. Each indexed item must be a map, slice, or array.
1 Like

what if it’s an associative array and having got the index i want the value of a property?
eg. $array[4].title

Assuming the field must be exported (capitalized).

{{ (index $array 4).Title }}
2 Likes

Is there a reason why

{{ (index $paginator.Pagers 1).URL }}

works but

{{ (index $paginator.Pagers 2).URL }}

throws a stack trace from inside hugolib? I’m pretty sure the list I am testing with is more than 3 pages long …

@moorereason has given good solution but I also tried this

Store in a variable

{{ $myarray := index $array 4 }}

and then use it

{{$myarray.title}}