Go templates: How do I access array item (arr[2]) in templates?
Asked Answered
D

2

106

How do I access an array item (e.g. a[2]) in templates?

Whenever I do this I get "bad character U+005B '['"

{{ .a[2] }}
Dockage answered 5/7, 2015 at 21:33 Comment(0)
M
27

to access a field on an array item as go templating in *.yaml format :

{{ (index .Values.fields 0).fieldItem }}

Index of zero(0) as in fields array of field as fieldItem .

Maxillary answered 14/1, 2021 at 14:34 Comment(0)
O
169

You need to use the index template function.

{{index .a 2}}
Odom answered 5/7, 2015 at 21:37 Comment(3)
To access a field on an array item use {{ (index .a 2).field }}.Kassala
@Kassala what if field is an variable? .$field doesn't workWolfenbarger
@Wolfenbarger Wrap it in another index {{ index (index .a 2) $field }}Pteridology
M
27

to access a field on an array item as go templating in *.yaml format :

{{ (index .Values.fields 0).fieldItem }}

Index of zero(0) as in fields array of field as fieldItem .

Maxillary answered 14/1, 2021 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.