0
0
Vueframework~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue v-for Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Vue component?

Consider this Vue 3 component using v-for to loop over an object:

<template>
  <ul>
    <li v-for="(value, key) in userInfo" :key="key">
      {{ key }}: {{ value }}
    </li>
  </ul>
</template>

<script setup>
const userInfo = {
  name: 'Alice',
  age: 30,
  city: 'Paris'
}
</script>

What will be the rendered list items?

Vue
<template>
  <ul>
    <li v-for="(value, key) in userInfo" :key="key">
      {{ key }}: {{ value }}
    </li>
  </ul>
</template>

<script setup>
const userInfo = {
  name: 'Alice',
  age: 30,
  city: 'Paris'
}
</script>
A<li>name: Alice</li><li>city: Paris</li><li>age: 30</li>
B<li>Alice: name</li><li>30: age</li><li>Paris: city</li>
C<li>name: Alice</li><li>age: 30</li><li>city: Paris</li>
D<li>0: name</li><li>1: age</li><li>2: city</li>
Attempts:
2 left
💡 Hint

Remember that v-for with objects iterates over keys and values in the order they are defined.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses v-for to iterate over an object?

Given the object const data = {a: 1, b: 2}, which Vue template snippet correctly iterates over it?

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

Remember the correct order of arguments in v-for when iterating objects.

🔧 Debug
advanced
2:00remaining
Why does this Vue component throw an error?

Examine this Vue 3 component snippet:

<template>
  <div>
    <p v-for="(key, value) in info" :key="key">{{ key }} - {{ value }}</p>
  </div>
</template>

<script setup>
const info = { x: 10, y: 20 }
</script>

What causes the error?

Vue
<template>
  <div>
    <p v-for="(key, value) in info" :key="key">{{ key }} - {{ value }}</p>
  </div>
</template>

<script setup>
const info = { x: 10, y: 20 }
</script>
AThe :key attribute cannot use 'key' when iterating objects.
BVue does not support v-for on objects, only arrays.
CThe object 'info' is not reactive, causing the error.
DThe order of arguments in v-for is reversed; it should be (value, key) in info.
Attempts:
2 left
💡 Hint

Check the order of the variables inside the parentheses in v-for.

state_output
advanced
2:00remaining
What is the value of count after this Vue component runs?

Given this Vue 3 component:

<template>
  <div>
    <p v-for="(val, key) in items" :key="key">{{ key }}: {{ val }}</p>
    <button @click="increment">Increment</button>
    <p>Count: {{ count }}</p>
  </div>
</template>

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

const items = reactive({ a: 1, b: 2 })
const count = ref(0)

function increment() {
  for (const key in items) {
    items[key]++
    count.value++
  }
}
</script>

If the user clicks the Increment button once, what is the value of count?

Vue
<template>
  <div>
    <p v-for="(val, key) in items" :key="key">{{ key }}: {{ val }}</p>
    <button @click="increment">Increment</button>
    <p>Count: {{ count }}</p>
  </div>
</template>

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

const items = reactive({ a: 1, b: 2 })
const count = ref(0)

function increment() {
  for (const key in items) {
    items[key]++
    count.value++
  }
}
</script>
A2
B1
C0
D3
Attempts:
2 left
💡 Hint

Count increases once for each key in the object during the loop.

🧠 Conceptual
expert
2:00remaining
Which statement about v-for with objects in Vue 3 is TRUE?

Choose the correct statement about using v-for to iterate over objects in Vue 3.

AThe order of iteration over object keys is guaranteed to be the same as insertion order in all browsers.
BYou must always provide a unique <code>:key</code> when using <code>v-for</code> with objects to avoid rendering issues.
CVue 3 automatically converts objects to arrays internally when using <code>v-for</code>.
DYou cannot access both key and value simultaneously in <code>v-for</code> when iterating objects.
Attempts:
2 left
💡 Hint

Think about best practices for rendering lists in Vue.