0
0
Vueframework~20 mins

v-for with index in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue v-for Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of v-for with index in Vue

Consider the following Vue template snippet:

<ul>
  <li v-for="(item, index) in items" :key="index">
    {{ index }} - {{ item }}
  </li>
</ul>

If items is ["apple", "banana", "cherry"], what will be the rendered list items?

Vue
<ul>
  <li v-for="(item, index) in items" :key="index">
    {{ index }} - {{ item }}
  </li>
</ul>
A<li>0 - apple</li><li>1 - banana</li><li>2 - cherry</li>
B<li>1 - apple</li><li>2 - banana</li><li>3 - cherry</li>
C<li>apple - 0</li><li>banana - 1</li><li>cherry - 2</li>
D<li>apple</li><li>banana</li><li>cherry</li>
Attempts:
2 left
💡 Hint

Remember that index starts at 0 in v-for.

📝 Syntax
intermediate
1:30remaining
Correct syntax for v-for with index

Which of the following is the correct syntax to use v-for with both item and index in Vue?

Av-for="items as item, index"
Bv-for="item, index in items"
Cv-for="(item, index) in items"
Dv-for="item in items, index"
Attempts:
2 left
💡 Hint

Check the parentheses and order of variables.

optimization
advanced
2:30remaining
Best key usage with v-for and index

Given a list of items that may change order or content, which key binding is best to avoid rendering bugs?

<li v-for="(item, index) in items" :key="?">{{ item }}</li>
A:key="Math.random()"
B:key="index"
C:key="item"
D:key="item.id"
Attempts:
2 left
💡 Hint

Keys should uniquely identify items and remain stable.

🔧 Debug
advanced
2:00remaining
Why does this v-for with index cause a warning?

Consider this code:

<div v-for="(item, index) in items">
  <p>{{ index }}: {{ item }}</p>
</div>

Vue shows a warning about missing key. Which option fixes it correctly?

Vue
<div v-for="(item, index) in items">
  <p>{{ index }}: {{ item }}</p>
</div>
AAdd :key="item" to the <p>
BAdd :key="index" to the <div>
CAdd :key="index" to the <p>
DNo need to add key, just ignore the warning
Attempts:
2 left
💡 Hint

Keys must be on the element with v-for.

🧠 Conceptual
expert
3:00remaining
Effect of using index as key in dynamic lists

What is the main problem when using the index as the key in a v-for loop for a list that can have items inserted or removed?

AVue may reuse DOM elements incorrectly, causing UI bugs
BVue will throw a runtime error and stop rendering
CVue will ignore the key and render items without keys
DVue will duplicate items in the list
Attempts:
2 left
💡 Hint

Think about how keys help Vue track elements.