Files
hugo/docs/content/en/functions/collections/IndexFunction.md
T
Bjørn Erik Pedersen 30a20122b7 Merge commit '8f3c066d23f431fb2c53d97ea489e4c28b42bd82'
# Conflicts:
#	docs/data/docs.yaml
2026-02-14 12:14:33 +01:00

1.2 KiB

title, description, categories, keywords, params, aliases
title description categories keywords params aliases
collections.Index Returns an element or value from the given slice or map at the specified key(s).
functions_and_methods
aliases returnType signatures
index
any
collections.Index SLICE|MAP KEY...
/functions/index
/functions/index-function

Each indexed item must be a map or a slice:

{{ $s := slice "a" "b" "c" }}
{{ index $s 0 }} → a
{{ index $s 1 }} → b

{{ $m := dict "a" 100 "b" 200 }}
{{ index $m "b" }} → 200

Use two or more keys to access a nested value:

{{ $m := dict "a" 100 "b" 200 "c" (slice 10 20 30) }}
{{ index $m "c" 1 }} → 20

{{ $m := dict "a" 100 "b" 200 "c" (dict "d" 10 "e" 20) }}
{{ index $m "c" "e" }} → 20

You may also use a slice of keys to access a nested value:

{{ $m := dict "a" 100 "b" 200 "c" (dict "d" 10 "e" 20) }}
{{ $s := slice "c" "e" }}
{{ index $m $s }} → 20

Use the collections.Index function to access a nested value when the key is variable. For example, these are equivalent:

{{ .Site.Params.foo }}

{{ $k := "foo" }}
{{ index .Site.Params $k }}