I have a colors object, that contains exactly what you think:
{
"RD": "Red",
"BL": "Blue",
"GR": "Green",
"YW": "Yellow"
}
I have a select dropdown that creates an <option> for each color in the colors object:
<select v-model="colors">
<option selected>SELECT A COLOR</select> // default value
<option v-for="(color, key) in colors">{{ color }}</option>
</select>
This displays great in the dropdown.
- The
{{ color }}expression shows the name, i.e. 'Blue' - If I were to instead use
{{ key }}as the expression, it would read 'BR'
Challenge:
- I am struggling to assign
keyto theoptionvalueattribute
For example:
<option v-for="(color, key) in colors" :key="key">{{ color }}</option>
In this case key is actually 'Blue' instead of 'BR', and switching it to color shows the same thing. What am I doing wrong?