DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Building a Real-Time Change Data Capture Pipeline With Debezium, Kafka, and PostgreSQL
  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes

Trending

  • The Future of Java and AI: Coding in 2025
  • Scaling Microservices With Docker and Kubernetes on Production
  • Event Driven Architecture (EDA) - Optimizer or Complicator
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  1. DZone
  2. Data Engineering
  3. Data
  4. How To Use V-for in Vue

How To Use V-for in Vue

Let's look at how v-for works in Vue.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Mar. 03, 22 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
8.8K Views

Join the DZone community and get the full member experience.

Join For Free

Often times when we are creating an application, the data we use determines what we show to the user. For example, in a to-do application, we may have multiple to-do list items. In Vue, it is easy to display multiple data points through the v-for attribute in our Vue templates.

How To Use V-for in Vue

Let's suppose we have some data we are storing on a single page component. Our .vue document looks a bit like this:

Vue.js Component
 
<template>
    <div id="locations">

    </div>
</template>
<script>
export default {
    data() {
        return {
            locations: [
                { name: 'London', date: '11/02/2022', numberOfPeople: 4, complete: true },
                { name: 'Paris', date: '12/01/2022', numberOfPeople: 2, complete: true },
                { name: 'Tokyo', date: '04/06/2021', numberOfPeople: 6, complete: true },
                { name: 'Mumbai', date: '08/10/2021', numberOfPeople: 10, complete: true },
                { name: 'New York', date: '12/12/2022', numberOfPeople: 14, complete: true },
                { name: 'Dubai', date: '10/02/2023', numberOfPeople: 12, complete: false },
                { name: 'Shanghai', date: '04/02/2020', numberOfPeople: 2, complete: true }
            ]
        }
    }
}
</script>

Our intention is to display all of our "location" data in our template. We are using static data here - but v-for is reactive, just like all of Vue. So if an API updates this data, it'll feed through to our template.

In this example, using v-for is a no-brainer. All we have to do is update or <template> tag to loop through each item. Let's take a look at how we'd do that:

HTML
 
<template>
    <div id="locations">
        <div class="location-item" v-for="(item, index) in locations" :key="index">
            <p>We travelled to {{ item.name }} on {{ item.date }} with {{ item.numberOfPeople }} people.</p>
        </div>
    </div>
</template>

After updating our code, we should have something that looks like this:

Example of the v-for loop in Vue.JS
So now all of our data is easily displayed in paragraph form. Our .location-item div fully contains the logic for our v-for loop:

HTML
 
<div class="location-item" v-for="(n, i) in locations" :key="i">

When we say (item, index) in locations, item refers to one item in our loop - so we can call item.name to get London, or Mumbai from our data set. index refers to the index of that element.

You might also notice we wrote :key="index". Every v-for loop item requires a key. For this example, we are using the index as our key. If you leave this out - you will get an error in Vue.

How To Use V-if and V-for Together in Vue

In Vue, we can't use v-for and v-if together, as they often conflict. In our data above, we have a field called completed, which is true if the journey is over, and false if it hasn't happened yet. If we wanted to only show completed journeys, we need to add our v-if to a child HTML element. If we add it to the element with v-for, it won't work!

As such, we could hide any element where completed is false by adding changing our template to look like this:

HTML
 
<template>
    <div id="locations">
        <div class="location-item" v-for="(item, index) in locations" :key="index">
            <p v-if="item.completed === true">We travelled to {{ item.name }} on {{ item.date }} with {{ item.numberOfPeople }} people.</p>
        </div>
    </div>
</template>

Nested V-for Loops in Vue

It's worth also mentioning that nested v-for loops are possible, and follow the same pattern as we've covered in this article. Here is an example of a set of nested v-for loops, where we loop through a list of countries, and their states:

Vue.js Component
 
<template>
    <div id="countries">
        <div class="country-item" v-for="(item, index) in countries" :key="index">
            <h2>{{ item.name }} States:</h2>
            <p v-for="(state, i) in item.states" :key="i">state.name</p>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            countries: [
                { 
                    name: "UK", 
                    states: [{
                        name: "London",
                        lowerCaseName: "london"
                    },
                    {
                        name: "Scotland",
                        lowerCaseName: "scotland"
                    }]
                    // More...
                },
                {
                    name: "India",
                    states: [{
                        name: "Madhya Pradesh",
                        lowerCaseName: "madhya-pradesh"
                    }]
                    // More...
                }
            ]
        }
    }
}
</script>

Example of a nested v-for loop in Vue.JS

Conclusion

That's all for v-for loops. We've looked at:

  1. How to use v-for loops on data().
  2. How to use nested v-for loops.
  3. Combining v-for and v-if.

Vue is super fun to use once you start.

Data (computing)

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a Real-Time Change Data Capture Pipeline With Debezium, Kafka, and PostgreSQL
  • Supervised Fine-Tuning (SFT) on VLMs: From Pre-trained Checkpoints To Tuned Models
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!