Return first value of map without ranging

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.

       key1:value1 key2:value2 key3:value3 key4:value4

Try out the index function

@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

select 1 as key


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?

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

the doc had the values [1][2]… so I had taken the 1, as old programmer I would start with 0 too.

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 )}} />

<img src={{ ( index $locationOfImage ( int $indexWhichisInString ))}} />