How to set a switch default value to the first index, not knowing the actual values

Hello,

Which expression should I use in order to set a switch default value to the first index, no matter what values the switch is currently holding ? I don’t really care if the values are not sorted, I just need one to be selected.

In my case I can’t know the key and value in advance but the switch is never empty since its values are set by an external script which rewrites the .json file.

You can set the switch’s default property as follows:

JS{{
return Object.values(@{this.values})[0]
}}

Note that the default property is only applied when loading the json file.

1 Like

Merci beaucoup pour la réponse super rapide !

This only works if there is more than 1 value within @{this.values}, at least for me. If the list length is only one, I'll always get just empty, blank default switch displayed. If I interact I can choose the one and only existing value.. hmm any suggestions what's going on here? How to correctly always choose the first one as default value? Sorry I can't get it ..

by the way: I use an menu instead of a switch with values set to a list variable:

JS{{
return @{variable_mylist}
}}

The list contains strings like ["test1","test2", ..]

If length(variable_mylist) == 1 the menu is blank (no default value set) - why is that?

if length > 1 it works ..

Which version are you using ? It works fine on the latest release.

JS{{
return Object.values(@{this.values})[0]
}}

If values is a array (["a", "b"]) instead of an object ({"label a": "a", "label b": "b"}), it can be simpler:

JS{{
  return @{this.values}[0]
}}

And shorter if want:

#{@{this.values}[0]}