Efficient Looping in VueJS: How to Iterate Between Two Numbers

How can I efficiently set values for select options using a loop with the v-for directive?

For instance, how can I dynamically fill in years between 1900 and 2010?

   <option v-for="year in (1900 to 2010)" :value="year"> {{ year }}

Answer №1

<li v-for="i in (2015 - 1988 + 1)" :value="i + 1988"> {{ i + 1988 }} </li>

You have the flexibility to adjust the range by changing the starting and ending numbers.

 <li v-for="i in (endNum - startNum + 1)" :value="i + startNum"> {{ i + startNum }} </li>

Demo: https://jsfiddle.net/mmbq7xpx/

Answer №2

It is my preference to create such objects in a controller instead of storing business logic in a template.

I opted for using lodash's _.range method to generate the range array.

const min = 1900;
const max = 2010;
    
new Vue({
  el: '#app',
  data: {
    years: _.range(min, max + 1)
  }
})
<script src="https://unpkg.com/vue"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

<div id="app">
  <select>
    <option v-for="i in years" :value="i"> {{ i }}</option>
  </select>
</div>

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

Capture XMLHttpRequest request and manually send a 200 status response using vanilla JavaScript

After extensive research, I found conflicting information on this topic. I need to intercept an XMLHttpRequest and simulate a 200 status response from the backend using plain Javascript. This is specifically for testing purposes. So far, I have made pro ...

What are some ways I can condense this code?

I have been working on improving my coding skills by learning jQuery. One of the tasks I tackled was creating a simple image showcase. However, the code I ended up with seems lengthy and not very efficient. Can anyone provide suggestions on how to enhance ...

The result shows me [object Object]

I'm struggling to find the solution to my issue - the output I am getting is incorrect and I can't pinpoint what's wrong. It's possible that some part of the code is missing, but as a learner, I'm not entirely sure. My goal is to ...

Ensure the ajax call finishes before the body loads

According to w3schools, there are different ways an external script can be executed: If async is used: The script runs asynchronously with the rest of the page If async is not used but defer is present: The script executes after the page has finished par ...

What is the method for implementing an Inset FAB with Material UI in a React project?

Currently, I am working on a project that requires an "Inset Fab" button to be placed between containers. After referencing the Material Design documentation, I discovered that the component is officially named "Inset FAB". While I was able to find some tu ...

The function req.isAuthenticated consistently comes back as false

if (process.env.NODE_ENV !== "production") { require("dotenv").config(); } const express = require("express"); const app = express(); const bcrypt = require("bcrypt"); const passport = ...

Calculate the total of each column in ng-table using AngularJS framework

Currently, I am utilizing angular/ng-table to display numerical data in a tabular format. However, one issue I am facing is how to add a row at the bottom of my table that displays the sum of all values in each column. While I can compute this sum on the s ...

Issues are arising within the script tag due to ejs tags

I have been attempting to incorporate ejs tags within an internal script tag in an ejs template. The code seems to be functioning correctly, however, "vs code" continues to indicate a "problem" with the "<%- and %>" segments as they appear red when h ...

Is the handlebars template to be rendered if the field is equal to the variable stored in req

Let's break it down: {#if creatorField == req.locals.user) <div class="thisHTML">{{ story }}</div> {else} <div class="thatHTML">{{ story }}</div> {/if} In case the creatorField doesn't match the value of req.locals.u ...

Check an image preview prior to uploading through FileReader, rotates the image

After browsing through numerous posts about viewing images before uploading, I stumbled upon an intriguing solution that claimed to be simple using FileReader: function displayImage(input) { if (input.files && input.files[0]) { var reader = ne ...

Is VueJS2 using the computed property/method to modify Vue data?

My form in Vue is almost complete, but I'm facing an issue with an array that seems to be affecting my Vue.data object directly. Here's the situation: In my code, I have a method called getParams() which returns an object containing all the data ...

Discover a method to conceal an element within a <div> by monitoring mouseover events in a separate <div> container

<div id="one-id"> <div id="some">Information</div> <div id="control"> <div id="value-1"> <img id="image-one-id" /> <img id="image-two-id" /> ...

The application's view page is not receiving a response from ajax

While debugging in the Firefox console window, I am able to see the output. However, it is not displaying on my view page. Below you will find my ajax code and controller function. Even though I am receiving a result, the view remains blank. Ajax: <sc ...

Layer one image on top of another using z-index

I'm having trouble layering one image on top of another in my code. Here is my code: body { background: #000000 50% 50%; height: 100% width:100%; overflow-x: hidden; overflow-y: hidden; } .neer { z-index: 100; position: absolute; } ...

Close dynamic overlay modal when client-side validation fails during submission

When a user clicks on the "Apply" button after opening a "confirm" modal, client-side validation checks all fields. However, in case of failure, the modal does not close and I am struggling to find a solution. I attempted to use an event handler on the su ...

View a pink map on Openlayers 2 using an iPhone

I am currently working on a project where I am trying to track my location using my smartphone and display it on a map. To achieve this, I am utilizing openlayers 2. However, I am encountering an issue. When I implement the code below in a Chrome Browser ...

How can I retrieve the name of the upcoming middleware function in Express.JS?

Hey there, I'm currently facing a challenge with retrieving the names of middleware functions in a specific request route. Let's consider the code snippet below as an example: const authorizeRoute = (req,res,next) => { let nextFunctionName = ...

Converting HTML entities to actual characters in Angular2: How to change apostrophe's code to apostrophe symbol

This is the text I have stored in my database: https://i.sstatic.net/vr3op.png When this text is accessed in an angular2 application and rendered, it displays HTML numbers. For example, a single quote(') appears as "HTML Number '" If you need ...

Navigating with parameters in Ionic using navCtrl.push()

When calling a method from the home.html file, I have encountered an issue. (click)="openPage(EventsPage)" I understand that using this method alone will work: openPage() { this.navCtrl.push(EventsPage) } What I want to achieve is to handle different ...

How to target individual DOM elements using their id within an ng-repeat loop in AngularJS

In a unique scenario I find myself in, there is a need to modify the CSS within a controller. While not typically recommended, it is necessary for this specific situation as outlined below. Within an ng-repeat loop, I have a title and description housed i ...