The v-for directive helps you show many items on the page by repeating a template for each item in a list.
v-for for list rendering in Vue
<template>
<ul>
<li v-for="item in items" :key="item.id">
{{ item.text }}
</li>
</ul>
</template>
<script setup>
import { ref } from 'vue'
const items = ref([{ id: 1, text: 'Example' }])
</script>Always add a unique :key to each item for better performance and to help Vue track items.
You can use v-for="(item, index) in items" to get the item's position too.
<ul> <li v-for="fruit in fruits" :key="fruit"> {{ fruit }} </li> </ul>
<ul> <li v-for="(user, index) in users" :key="user.id"> {{ index + 1 }}. {{ user.name }} </li> </ul>
<ul> <li v-for="item in []" :key="item.id"> {{ item.text }} </li> </ul>
This Vue component shows a shopping list. It uses v-for to repeat each product as a list item with its name and quantity.
<template>
<div>
<h2>My Shopping List</h2>
<ul>
<li v-for="product in products" :key="product.id">
{{ product.name }} - {{ product.quantity }} pcs
</li>
</ul>
</div>
</template>
<script setup>
import { ref } from 'vue'
const products = ref([
{ id: 101, name: 'Apples', quantity: 5 },
{ id: 102, name: 'Bananas', quantity: 3 },
{ id: 103, name: 'Carrots', quantity: 4 }
])
</script>Time complexity: Rendering with v-for is proportional to the number of items in the list.
Space complexity: Uses memory for the list and the rendered elements.
Always use a unique :key to avoid rendering bugs and improve updates.
Use v-for when you want to show many similar items. For single items, just use normal bindings.
v-for repeats a template for each item in a list.
Always add a unique :key for each item.
You can access the item and its index in the loop.
Practice
v-for directive do in Vue.js?Solution
Step 1: Understand the purpose of
Thev-forv-fordirective is used to loop over arrays or objects and render elements multiple times.Step 2: Compare with other directives
Other directives likev-ifcontrol visibility,v-bindbinds data, andv-onlistens to events, so they do not repeat elements.Final Answer:
It repeats an element or component for each item in a list. -> Option AQuick Check:
v-for= repeat elements [OK]
- Confusing v-for with v-if for conditional rendering
- Thinking v-for binds data instead of looping
- Mixing v-for with event listeners like v-on
v-for in Vue?Solution
Step 1: Identify correct
The correct syntax isv-forsyntaxv-for="item in items"whereitemsis the array anditemis the loop variable.Step 2: Check each option
<li v-for="item in items">{{ item }}</li>uses correct syntax and variable names. Options B, C, and D have incorrect order or variable names.Final Answer:
<li v-for="item in items">{{ item }}</li> -> Option CQuick Check:
Usev-for="item in items"[OK]
- Swapping 'item' and 'items' in the syntax
- Using 'of' instead of 'in' (Vue uses 'in')
- Using plural for loop variable instead of singular
<ul>
<li v-for="(fruit, index) in fruits" :key="index">{{ index }} - {{ fruit }}</li>
</ul>and the data:
fruits: ['Apple', 'Banana', 'Cherry']
What will be the rendered output?
Solution
Step 1: Understand the loop variables
Thev-foruses(fruit, index) in fruits, soindexis the position andfruitis the item.Step 2: Substitute values in template
Each- shows "index - fruit". For the array ['Apple', 'Banana', 'Cherry'], indexes are 0,1,2 respectively.
Final Answer:
<ul><li>0 - Apple</li><li>1 - Banana</li><li>2 - Cherry</li></ul> -> Option DQuick Check:
Index and fruit displayed correctly [OK]
- Mixing order of index and item in output
- Forgetting to use :key with index
- Assuming 'of' works instead of 'in'
<ul>
<li v-for="item in items">{{ item }}</li>
</ul>Given the data:
items: null
What will happen when Vue tries to render this?
Solution
Step 1: Understand v-for expects iterable data
Thev-fordirective requires an array or object to loop over. Ifitemsisnull, it is not iterable.Step 2: Predict Vue behavior on null
Vue will throw a runtime error because it cannot iterate overnull.Final Answer:
It will throw a runtime error because items is null. -> Option BQuick Check:
v-for needs iterable data [OK]
- Assuming null renders empty list silently
- Expecting v-for to handle null gracefully
- Confusing null with empty array []
const usersByCity = {
'New York': [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }],
'Paris': [{ id: 3, name: 'Claire' }]
}How do you use
v-for to render a list showing city names and their users' names inside each city?Solution
Step 1: Understand the data structure
usersByCityis an object where keys are city names and values are arrays of user objects.Step 2: Use
Usev-forto loop over object entriesv-for="(users, city) in usersByCity"to get city name and users array. Then loop over users array inside.Step 3: Check options for correct variable order and keys
<div v-for="(users, city) in usersByCity" :key="city"> <h3>{{ city }}</h3> <ul> <li v-for="user in users" :key="user.id">{{ user.name }}</li> </ul> </div>correctly uses(users, city)and nestedv-forwith keys.<div v-for="(city, users) in usersByCity" :key="city"> <h3>{{ city }}</h3> <ul> <li v-for="user in users" :key="user.id">{{ user.name }}</li> </ul> </div>swaps city and users order incorrectly. Options B and D misuse variables.Final Answer:
Option A code snippet correctly renders cities and their users. -> Option AQuick Check:
Object loop: (value, key) = (users, city) [OK]
- Swapping key and value order in v-for
- Using array syntax for object iteration
- Missing unique :key on nested loops
Ready to prove it?
Pass the test and this pattern moves unknown
