Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

85
Vistas
Vue multiple options per loop

Trying to display multiple options in Vue2 for loop of a select.

Given an object with the structure;

sorting = {
    name: [
        'asc',
        'desc'
    ],
    price: [
        'cheapest',
        'expensive'
    ]
}

How to format the loop to get the output of;

<select>
    <option value="name:asc">name asc</option>
    <option value="name:desc">name desc</option>
    <option value="price:cheapest">name cheapest</option>
    <option value="price:expensive">name expensive</option>
</select>

Can't work out the way to show the second option;

<select id="sortBy" class="form-control pr-3" v-model="sortBy">
    <option
        v-for="(options, sort) in sorting" :key="`${sort}`"
        v-bind:value="`${sort}:${options[0]}`"
    >
        {{ sort }} {{ options[0] }}
    </option>
</select>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

That is because your sorting data contains nested arrays: you need to iterate through each keys (you've done done), and the also iterate through the array in each key to generate the options.

My suggestion is that instead of trying to do that in the template, which can be verbose and less readable... you can abstract that into a computed property that handles the option generation for you.

The trick here is simply using Object.entries() (which returns a key and value tuple) and then iterating through the value to return the key:arrayValue combination. Use Array.prototype.flatMap to flatten the returned array:

new Vue({
  el: '#app',
  data: {
    sorting: {
      name: [
        'asc',
        'desc'
      ],
      price: [
        'cheapest',
        'expensive'
      ]
    },
    sortBy: '',
  },
  computed: {
    options() {
      return Object.entries(this.sorting).flatMap(([key, optionsByKey]) => {
        return optionsByKey.map(v => `${key}:${v}`);
      });
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <select id="sortBy" class="form-control pr-3" v-model="sortBy">
    <option v-for="(option, i) in options" :key="i" :value="option">
      {{ option.replace(':', ' ') }}
    </option>
  </select>
  <br /><br />
  Selected value: {{ sortBy }}
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use two option loops.

<select id="sortBy" class="form-control pr-3" v-model="sortBy">
    <option
        v-for="n in options.name" :key="`name:${n}`"
        :value="`name:${n}`"
    >
        name {{ n }}
    </option>
    <option
        v-for="p in options.price" :key="`price:${p}`"
        :value="`price:${p}`"
    >
        price {{ p }}
    </option>
</select>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda