Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Vue Tutorial

Vue HOME Vue Intro Vue Directives Vue v-bind Vue v-if Vue v-show Vue v-for Vue Events Vue v-on Vue Methods Vue Event Modifiers Vue Forms Vue v-model Vue CSS Binding Vue Computed Properties Vue Watchers Vue Templates

Scaling Up

Vue Why, How and Setup Vue First SFC Page Vue Components Vue Props Vue v-for Components Vue $emit() Vue Fallthrough Attributes Vue Scoped Styling Vue Local Components Vue Slots Vue v-slot Vue Scoped Slots Vue Dynamic Components Vue Teleport Vue HTTP Request Vue Template Refs Vue Lifecycle Hooks Vue Provide/Inject Vue Routing Vue Form Inputs Vue Animations Vue Animations with v-for Vue Build Vue Composition API

Vue Reference

Vue Built-in Attributes Vue Built-in Components Vue Built-in Elements Vue Component Instance Vue Directives Vue Instance Options Vue Lifecycle Hooks

Vue Examples

Vue Examples Vue Exercises Vue Quiz Vue Server Vue Certificate

Vue v-for Directive


Example

Using the v-for directive to create a list of mammals, based on an array:

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive to create a list of mammals based on an array.</p>
  <ul>
    <li v-for="x in animals">{{ x }}</li>
  </ul>
</template>
Run Example »

See more examples below.


Definition and Usage

The v-for directive is used to render multiple elements based on a data source.

The v-for directive is used with a syntax "(item, key, index) in dataSource", where:

  • The "item" alias represents an element inside the "dataSource".
  • The "key" alias can be used to get the property names if the data source is an object.
  • The "index" alias can be used if the data source is an array or an object.
  • The "dataSource" must be the name of the actual data source you are looping through.

You can choose the names of the "item", "key" and "index" aliases yourself, but the order is "item, key, index".

These are the data sources that can be used by the v-for directive:

Data Source Type Details
Array v-for loops through the array, and the element and the index of each element can be picked out and used. Run Example »
Object v-for loops through the Object. The property names, values and indexes can be picked out and used. Run Example »
number v-for renders a list, where each item is a number from one, and last number is the number provided. The index of each element can also be picked out. Run Example »
string v-for loops through the string. Each character and its index can be picked out and used. Run Example »
Iterable v-for can also loop through iterables. Iterables are values that use the Iterable Protocol, like Map and Set. Run Example »

Note: To optimize performance, Vue reuses elements created with v-for when the data source gets manipulated. This can lead strange results (explained here). To prevent Vue from reusing elements wrongfully when using v-for, you should always use the special key attribute with v-bind, to mark each element uniquely (see Example 6).


More Examples

Example 1

Using the v-for directive to render a list of mammals, based on an array, and also picking the index of each element in the array:

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive to create a list of mammals, and the index of each mammal, based on an array.</p>
  <ul>
    <li v-for="(x, index) in animals">On index {{ index }}: "{{ x }}"</li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      animals: ['Tiger','Zebra','Wolf','Crocodile','Seal']
    };
  }
};
</script> 
Run Example »

Example 2

Using the v-for directive to render a list of properties, picking out the property name and value for every property in an object:

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive on an Object to create a list of the object properties and the respective property values.</p>
  <ul>
    <li v-for="(x, key) in animal">(Property name: value) = ({{ key }}: {{ x }})</li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      animal: {
        name: 'Lion',
        heightCM: 110,
        weightKG: 150
      }
    };
  }
};
</script>
Run Example »

Example 3

Using the v-for directive to render a list based on a number:

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive with number to render a list with that number of elements.</p>
  <ul>
    <li v-for="(x, index) in 10">Item: {{ x }}, index: {{ index }}</li>
  </ul>
</template>
Run Example »

Example 4

Using the v-for directive to loop through a string of characters:

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive to loop through the characters in a string.</p>
  <ul>
    <li v-for="(x, index) in 'Ice cream'">Item: "{{ x }}", index: {{ index }}</li>
  </ul>
</template>
Run Example »

Example 5

Using the v-for directive to loop through an object created with the Iterable Protocol:

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive  to render a list, based on an object created with the Iterable Protocol.</p>
  <ul>
    <li v-for="value in iterableObject">{{ value }}</li>
  </ul>
</template>

<script>
export default {
  data() {
    return {
      iterableObject: this.createIterable(['City', 'Park', 'River'])
    };
  },
  methods: {
    createIterable(array) {
      let currentIndex = -1;
      return {
        [Symbol.iterator]: function () {
          return {
            next: () => {
              if (currentIndex < array.length - 1) {
                currentIndex++;
                return { value: array[currentIndex], done: false };
              } else {
                return { done: true };
              }
            }
          };
        }
      };
    }
  }
};
</script>
Run Example »

Example 6

Using the v-for directive to render one div element for every character in a string. It is always recommended tu use v-bind:key with the v-for directive:

<template>
  <h2>Example v-for Directive</h2>
  <p>Using the v-for directive with 'v-bind:key' to render DIV elements, based on a string of characters.</p>
  <div id="wrapper">
    <div v-for="x in text" v-bind:key="x">{{ x }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      text: 'I love ice cream.'
    };
  }
};
</script>

<style>
#wrapper {
  display: flex;
  flex-wrap: wrap;
  width: 280px;
}
#wrapper > div {
  margin: 5px;
  padding: 5px 10px;
  border: solid black 1px;
  background-color: lightgreen;
}
</style>
Run Example »

Related Pages

JavaScript Tutorial: JS Iterrables

Vue Tutorial: Vue v-for Directive

Vue Tutorial: Vue v-for Components

Vue Tutorial: Vue Animations with v-for

Vue Reference: Vue 'key' Attribute