Weru
October 17, 2019, 1:36pm
1
After using where
I’m left with an array boxed inside a map. I could use range
to obtain the array, but I wonder if there’s a better way to approach this since it’s only a single array.
[map
[
course:home
values:map[
key1:value1 key2:value2 key3:value3 key4:value4
]
]
]
zwbetz
October 17, 2019, 2:48pm
2
Try out the index
function
Weru
October 17, 2019, 3:07pm
3
@zwbetz , thanks for the response. I tried
{{ index $data "values" }}}
$data
is a variable holding the map in question. I got this error
<index $data "values">: error calling index: cannot index slice/array with type string
zwbetz
October 17, 2019, 3:42pm
5
As @ju52 mentioned, that function expects an integer as the second arg.
Edit: nevermind, looks like it also accepts a key as arg. So make sure you’re in the right place in your data structure. Do some print statements to debug.
1 Like
I am a little bit afraid that I misunderstand the question, but y’all are aware of the first
function, right?
wouldn’t {{ range first 1 $data.values }}
do the trick?
Weru
October 17, 2019, 4:45pm
7
This worked but the right key is 0 . key 1 , returns an empty collection. I think it’s zero based.
{{ index $data 0 }}
1 Like
ju52
October 17, 2019, 4:52pm
8
the doc had the values [1][2]… so I had taken the 1 , as old programmer I would start with 0 too.
Weru
October 17, 2019, 4:56pm
9
That’s okay, your tip helped a lot. Thanks!
use ( int $varName) instead 0 or 1
$varName can be in a string ; it will be converted in to int and run successfully
for example
//when u get error
<img src={{ ( index $locationOfImage $indexWhichisInString )}} />
//fix
<img src={{ ( index $locationOfImage ( int $indexWhichisInString ))}} />