What steps can I take to resolve the issue of item display in the dropdown menu?

My objective is to have a dropdown list with "title 1234" appearing first. When this option is selected, its value should be 1234. How can I achieve this?

Here is the desired outcome:

https://i.sstatic.net/CqWgn.png

Currently, the list looks like this:

https://i.sstatic.net/J61up.png

Vue.createApp({
        data: () => ({
            model: 'title 1234',
            options: {
                1234: 'title 1234',
                1: 'title 1',
                2: 'title 2',
                3: 'title 3',
                10: 'title 10',
            },
        })
    }).mount('#app')
<div id="app">
  <select v-model="model">
    <option v-for="option in options">
      {{ option }}
    </option>
  </select>
  <span>Value: {{ model }}</span>
</div>
    
<script src="https://unpkg.com/vue@next"></script>
<script src="app.js"></script>

Answer №1

To access both the key and the value within a loop, you can utilize the Map object along with its .entries() method in JavaScript

Vue.createApp({
    data: () => ({
        model: '1234',
        options: new Map([
            [ 1234, 'title 1234' ],
            [ 1, 'title 1' ],
            [ 2, 'title 2' ],
            [ 3, 'title 3' ],
            [ 10, 'title 10' ],
        ]),
    }),
}).mount('#app')
<div id="app">
    <select v-model="model">
        <option v-for="[ key, value ] of options.entries()" :value="key">
            {{ value }}
        </option>
    </select>
    <span>Value: {{ model }}</span>
</div>

<script src="https://unpkg.com/vue@next"></script>
<script src="app.js"></script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using multiple jQuery dialogs on index.php

I have a vision for my website to mirror the Windows environment, complete with icons that prompt dialog boxes when clicked. On my site's index page, I've added the following code within the head tags: <link rel="stylesheet" href="http://cod ...

How to adjust the minimum height of v-application--wrap in Vuetify

The layout of my page includes a vuetify component as a widget, followed by other content. However, there is too much space between the widget and the rest of the page, causing the vuetify app to take up more height than desired. I've attempted to adj ...

Transition smoothly between two images with CSS or jQuery

I am working on a project where I need to implement a hover effect that fades in a second image above the initial image. The challenge is to ensure that the second image remains hidden initially and smoothly fades in without causing the initial image to fa ...

Facing difficulty transferring an array from React to Django

Trying to transfer an array from the React frontend (stored in local storage) to my view class in Django is resulting in the following error: Console Output: GET http://127.0.0.1:8000/api/quiz/multiple/ 500 (Internal Server Error) Django Logs: for qu ...

The jquery fancybox ajax modal popup fails to display in Internet Explorer 7

Recently, I utilized JQuery fancy box for the first time to display a menu list. When clicking on a specific item, the page is rendered properly in an iframe. However, if there is already an AJAX model popup present on the rendered page, it doesn't di ...

When working on a React/Redux application, how do you decide between explicitly passing down a prop or retrieving it from the global state using mapStateToProps?

As I embark on creating my inaugural React-Redux application, a recurring decision I face is whether to employ <PaginationBox perPage={perPage} /> or opt for <PaginationBox /> and then implement the following logic: function mapStateToProps({p ...

Calculating the dot product of two arrays using JavaScript

let array1 = [2,4,6] let array2 = [3,3,3] let result = dotProduct(array1,array2) // should return 36 Can you suggest a streamlined approach to creating the dotProduct function without relying on external libraries? ...

What is the best way to ensure the website theme remains consistent after a refresh in React?

I am currently enhancing a weather forecast website by incorporating a theme toggler feature. The functionality has been successfully implemented, but I am facing an issue where the selected theme does not persist after reloading the page. Can someone he ...

Using ExtJS to populate a panel with data from various sources

I am currently working with an Ext.grid.GridPanel that is connected to a Ext.data.JsonStore for data and Ext.grid.ColumnModel for grid specifications. Out of the 10 columns in my grid, 9 are being populated by the json store without any issues. However, I ...

Creating a Vue template using Javascript is a straightforward process

My app has a specific scenario where I need to define a template using JavaScript. For instance, after importing my template import MyTemplate from "./MyTemplate"; export default { components: {MyTemplate}, } Is it possible to declare the tem ...

Having trouble making "jQuery.inArray" function properly in my code

Recently, I made a small interaction where list items are displayed and rotated when clicked - check it out here: http://jsfiddle.net/S79qp/430/ Lately, due to compatibility issues with IE8, I had to switch from using .indexOf() to jQuery.inArray. However ...

Guide to dynamically using array.map based on a condition in React

I am encountering an issue with a modal screen that contains two dropdowns and a text input field. The problem arises when the second dropdown is set to “is empty”, as the text input field should then disappear, leaving just the two dropdown inputs on ...

How to insert text into a text input using onKeyPress in react.js

I am looking to simulate a typing effect where the phrase 'Surveillance Capitalism' is inputted letter by letter into a text input field as a user types. However, I encounter an issue when trying to add each individual letter into the input; I re ...

Click event to focus on AngularJS TinyMCE textarea

My current project utilizes the tinymce text editor in combination with the angularJS framework. I obtained the directive from here and successfully integrated the editor using the provided example on GitHub! Initially, everything was working smoothly wit ...

Converting Firebase TIMESTAMP values to human-readable date and time

Utilizing Firebase in my chat application, I am adding a timestamp to the chat object using the Firebase.ServerValue.TIMESTAMP method. I want to display the time when the message was received in the chat application using this timestamp. If it is the cur ...

How to determine if a radio button has been selected using Javascript?

I have come across scripts that address this issue, however they are only effective for a single radio button name. My case involves 5 different sets of radio buttons. I attempted to check if it is selected upon form submit. if(document.getElementById(&ap ...

Displaying Product Attribute and Category Names in Woocommerce Title

After reading the answer provided in this thread (Woocommerce: How to show Product Attribute name on title when in a category page and "filtering" products via '?pa_attribute=' on address bar), I am interested in displaying both the categ ...

Upon migrating from Vue CLI 2 to 3, an error is thrown stating: "Cannot locate element: #app" and an empty body is

Currently, I am in the process of transitioning my VueJS project from VueCLI 2 to version 3. After moving all the necessary files to the src folder, I attempted to view it in the browser by running npm run serve. However, I encountered a problem where the ...

Developing a custom logging middleware with morgan

I am trying to develop a middleware using morgan. I have attempted to export the middleware function and then require it in app.js. However, I am facing an issue where it is not working as expected. Middleware: const morgan = require('morgan&apos ...

Error: Service Liferay encountered an unexpected identifier causing a syntax error

Upon clicking on the button labeled "delete save" in the Chrome console, an error is encountered: "Uncaught SyntaxError: Unexpected identifier". Additionally, when this action is performed, an object is printed to the console revealing its data ({company ...