0

I am strugling listing items with v-for on an associative array.

Don't know how to pass the key's of the array's and then list the titles.

Found a lot of examples, but didn't found a familiar example.

There are more language selections, but posted just a few so you could all get the idea.

Array:

data: () => ({
        translations: [
            {
                'lt': {
                    title: 'Lithuanian',
                },
                'en': {
                    title: 'English',
                }
            }
        ],
})

HTML:

<select v-model="activeLanguage">
  <option v-for="(translation, key) in translations" :value="key">
    {{translation.title}}
  </option>
</select>

How to specify the key and then display the titles in a v-for and in a HTML option tag?

1
  • Can you change the variable translations and edit it so it's an object and not an array? Commented May 12, 2020 at 14:58

2 Answers 2

1
<option v-for="(translation, key) in translations[0]" :value="key">
  {{ translation.title }}
</option>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It worked, and is very simple. Had a thought of doing this way, but didn't do it, lol.
1

You should iterate over translations[0] instead. Here is an example:

new Vue({
  el: "#app",
  data() {
    return {
         activeLanguage:null,
         translations: [
            {
                'lt': {
                    title: 'Lithuanian',
                },
                'en': {
                    title: 'English',
                }
            }
        ],
    };
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
     <select v-model="activeLanguage">
          <option v-for="(translation, key) in translations[0]" :value="key">
                {{translation.title}}
          </option>
     </select>
</div>

2 Comments

Thanks for your time and answer, but this is not the proper solution. There will be multiple languages, and the select purpess is to select the language and then change the page content by the selected language. Made is as simple as possible, so posted just the part where the problem is.
Thanks for your help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.