📖 Scenario: You are building a simple Vue app that shows a list of fruits. You want to display each fruit with its position number in the list.
🎯 Goal: Create a Vue component that uses v-for to loop over a list of fruits and display each fruit's name along with its index number starting from 1.
📋 What You'll Learn
Create a reactive array called fruits using ref with the exact array: ['Apple', 'Banana', 'Cherry', 'Date']
Add a v-for directive to loop over fruits with variables fruit and index
Display the index number starting from 1 before each fruit name
Use a unique key attribute with the index in the v-for loop
💡 Why This Matters
🌍 Real World
Displaying lists with position numbers is common in apps like to-do lists, rankings, or menus.
💼 Career
Understanding v-for with index and keys is essential for building dynamic lists in Vue applications.
Progress0 / 4 steps
1
Create the fruits array
Import ref from 'vue' and create a reactive array called fruits using ref. It should contain these exact strings: 'Apple', 'Banana', 'Cherry', 'Date'.
Vue
Hint
Use ref from Vue to create a reactive array called fruits.
2
Add the v-for loop with index
Inside the <div> in the template, add an unordered list <ul>. Inside it, add a list item <li> that uses v-for to loop over fruits with variables fruit and index. Use v-for="(fruit, index) in fruits".
Vue
Hint
Use v-for="(fruit, index) in fruits" on the <li> and add :key="index".
3
Display the index and fruit name
Inside the <li> tag, display the index number plus 1, followed by a dot and a space, then the fruit name. Use mustache syntax like {{ index + 1 }}. {{ fruit }}.
Vue
Hint
Use {{ index + 1 }}. {{ fruit }} inside the <li> to show the number and fruit.
4
Complete the Vue component
Make sure the Vue component has the full template with the v-for loop showing the index and fruit, and the fruits array defined in script setup using ref. The key attribute must be set to index on the <li>.
Vue
Hint
Check that all parts are included: the fruits array, the v-for with fruit and index, the key, and the display of index plus 1 and fruit.
Practice
(1/5)
1. What does the v-for directive do in Vue when used with a list?
easy
A. Creates a single element regardless of list size
B. Deletes items from the list
C. Sorts the list items alphabetically
D. Repeats an element for each item in the list
Solution
Step 1: Understand the purpose of v-for
The v-for directive is designed to repeat an element for each item in a list or array.
Step 2: Compare options with this behavior
Only Repeats an element for each item in the list describes repeating elements for each list item, which matches v-for usage.
Final Answer:
Repeats an element for each item in the list -> Option D
Quick Check:
v-for repeats elements [OK]
Hint: Remember: v-for loops over lists to repeat elements [OK]
Common Mistakes:
Thinking v-for deletes or sorts items
Confusing v-for with conditional rendering
Assuming v-for creates only one element
2. Which of the following is the correct syntax to use v-for with an index in Vue?
easy
A. v-for="(item, index) in items"
B. v-for="item, index in items"
C. v-for="item in items, index"
D. v-for="items as item, index"
Solution
Step 1: Recall correct v-for syntax with index
The correct syntax is v-for="(item, index) in items", where the item and index are inside parentheses.
Step 2: Check each option
Only v-for="(item, index) in items" uses parentheses correctly and the right order: item then index.
Final Answer:
v-for="(item, index) in items" -> Option A
Quick Check:
Parentheses around item and index = v-for="(item, index) in items" [OK]
Hint: Use parentheses around item and index in v-for [OK]
Common Mistakes:
Omitting parentheses around item and index
Placing index after the list instead of inside parentheses
Using commas outside parentheses
3. Given the Vue template snippet:
<ul>
<li v-for="(fruit, i) in ['apple', 'banana', 'cherry']" :key="i">{{ i }} - {{ fruit }}</li>
</ul>
What will be the rendered list items?
medium
A. 1 - apple 2 - banana 3 - cherry
B. 0 - apple 1 - banana 2 - cherry
C. apple - 0 banana - 1 cherry - 2
D. apple banana cherry
Solution
Step 1: Understand the loop and index
The loop goes over the array ['apple', 'banana', 'cherry'] with index i starting at 0.
Step 2: Check the output format
Each list item shows "{{ i }} - {{ fruit }}", so index then fruit name separated by a dash.
Final Answer:
0 - apple 1 - banana 2 - cherry -> Option B
Quick Check:
Index starts at 0, format is 'index - fruit' [OK]
Hint: Index in v-for starts at 0 by default [OK]
Common Mistakes:
Assuming index starts at 1
Swapping index and item in output
Ignoring the :key binding
4. Identify the error in this Vue snippet:
<div v-for="item, index in items" :key="index">
{{ index }}: {{ item }}
</div>
medium
A. Missing parentheses around item and index
B. Incorrect use of :key attribute
C. Using index instead of item in interpolation
D. v-for cannot be used on div elements
Solution
Step 1: Check v-for syntax
The syntax for v-for with index requires parentheses: v-for="(item, index) in items".
Step 2: Analyze the given code
The code uses v-for="item, index in items" without parentheses, which is invalid.
Final Answer:
Missing parentheses around item and index -> Option A
Quick Check:
Parentheses required for multiple variables in v-for [OK]
Hint: Always wrap item and index in parentheses in v-for [OK]
Which Vue template correctly uses v-for with index and sets a unique :key for each user?
hard
A. <div v-for="(user, i) in users" :key="i">{{ user.name }}</div>
B. <div v-for="user in users" :key="user.id">{{ user.name }}</div>
C. <div v-for="(user, index) in users" :key="user.id">{{ index }} - {{ user.name }}</div>
D. <div v-for="(user, index) in users" :key="index">{{ user.id }}: {{ user.name }}</div>
Solution
Step 1: Identify correct v-for syntax with index
Options A, B, and D use (user, index) syntax correctly; C does not use index.
Step 2: Check for unique :key usage
Keys should be unique and stable. Using user.id is best. <div v-for="(user, index) in users" :key="user.id">{{ index }} - {{ user.name }}</div> uses :key="user.id" with index, which is correct.
Step 3: Verify output format
<div v-for="(user, index) in users" :key="user.id">{{ index }} - {{ user.name }}</div> shows index and user name, which matches the question requirement.
Final Answer:
<div v-for="(user, index) in users" :key="user.id">{{ index }} - {{ user.name }}</div> -> Option C
Quick Check:
Use (item, index) with unique :key from item [OK]
Hint: Use item ID for :key, index for display only [OK]
Common Mistakes:
Using index as :key causing update issues
Not using parentheses for item and index
Ignoring unique keys requirement
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.