Using object as Vue Select options

1.4k Views Asked by At

I know, Vue Select docs specify that options should be an array, but is there a way around it? I want to use object keys as values and object values as labels.

My data:

obj: {
   value: 'en',
   options: {
     'ar': 'Arabic',
     'ast': 'Asturian',
     'en':' English'
   }
}
 <v-select                                       
       v-model="obj.value"
       :options="Object.keys(obj.options)"                                 
>

I know i can use keys as options that way, but I have no idea how to use values as labels. Any tips?

1

There are 1 best solutions below

0
On

There are multiple ways you could do that but one options is:

<v-select v-model="obj.value" :options="obj.options" :reduce="val => val.code"/>

Only change to your data should be that the obj.options should look like below:

obj: {
    value: "en",
    options: [
      { label: "Arabic", code: "ar" },
      { label: "Asturian", code: "ast" },
      { label: "English", code: "en" }
    ]
  }

Relevant documentation transforming-selections