0
0
Vueframework~20 mins

v-for for list rendering in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
v-for Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be rendered by this Vue template?

Consider the following Vue 3 template snippet using v-for:

<ul>
  <li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>

Given the data:

items = [
  { id: 1, name: 'Apple' },
  { id: 2, name: 'Banana' },
  { id: 3, name: 'Cherry' }
]

What will be the rendered HTML inside the <ul>?

Vue
<ul>
  <li v-for="item in items" :key="item.id">{{ item.name }}</li>
</ul>
A<ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul>
B<ul><li>1</li><li>2</li><li>3</li></ul>
C<ul><li>item.name</li><li>item.name</li><li>item.name</li></ul>
D<ul><li>Apple Banana Cherry</li></ul>
Attempts:
2 left
💡 Hint

Remember that v-for repeats the element for each item in the list, and {{ item.name }} outputs the name property.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses v-for with index?

In Vue 3, you want to render a list of names with their index number. Which of the following v-for syntaxes is correct?

A<li v-for="index, name in names" :key="index">{{ index }} - {{ name }}</li>
B<li v-for="name, index in names" :key="index">{{ index }} - {{ name }}</li>
C<li v-for="name of names, index" :key="index">{{ index }} - {{ name }}</li>
D<li v-for="(name, index) in names" :key="index">{{ index }} - {{ name }}</li>
Attempts:
2 left
💡 Hint

Remember the syntax for v-for with index is (item, index) in list.

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

Look at this Vue 3 template snippet:

<div v-for="item in items">
  {{ item.text }}
</div>

Given items is an array of objects, Vue shows a warning about missing key. Why?

Vue
<div v-for="item in items">
  {{ item.text }}
</div>
ABecause each element rendered by v-for needs a unique :key attribute for Vue to track changes efficiently.
BBecause the items array is empty and v-for cannot render anything.
CBecause the syntax of v-for is incorrect and missing parentheses.
DBecause the item.text property is undefined in the objects.
Attempts:
2 left
💡 Hint

Vue requires a special attribute on elements inside v-for loops to help with updates.

state_output
advanced
2:00remaining
What is the value of selected after clicking the second item?

Given this Vue 3 component snippet:

<template>
  <ul>
    <li v-for="(item, idx) in items" :key="item.id" @click="selected = idx">
      {{ item.name }}
    </li>
  </ul>
  <p>Selected: {{ selected }}</p>
</template>

<script setup>
import { ref } from 'vue'

const items = [
  { id: 1, name: 'Red' },
  { id: 2, name: 'Green' },
  { id: 3, name: 'Blue' }
]
const selected = ref(null)
</script>

If the user clicks the second list item (Green), what will be displayed in the <p>Selected:</p> paragraph?

ASelected: Green
BSelected: 2
CSelected: 1
DSelected: null
Attempts:
2 left
💡 Hint

The click sets selected to the index of the clicked item.

🧠 Conceptual
expert
2:00remaining
Why is using array index as :key in v-for discouraged?

In Vue 3, you can use :key to help Vue track list items. Why is it generally discouraged to use the array index as the key?

ABecause Vue does not allow numeric keys and will throw an error.
BBecause using index as key can cause rendering bugs when the list order changes or items are added/removed.
CBecause keys must be strings and index is a number.
DBecause using index as key slows down rendering performance significantly.
Attempts:
2 left
💡 Hint

Think about what happens if the list changes order but keys stay the same.