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?
<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>Remember that v-for with objects iterates over keys and values in the order they are defined.
The v-for directive loops over the userInfo object. The first argument is the value, the second is the key. So it renders each key and its corresponding value in order.
v-for to iterate over an object?Given the object const data = {a: 1, b: 2}, which Vue template snippet correctly iterates over it?
Remember the correct order of arguments in v-for when iterating objects.
The correct syntax for iterating over an object is (value, key) in object. Option A follows this pattern.
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?
<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>Check the order of the variables inside the parentheses in v-for.
When iterating objects, the first variable is the value, the second is the key. The code reverses this, causing Vue to misinterpret the variables and throw an error.
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?
<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>Count increases once for each key in the object during the loop.
The increment function loops over two keys ('a' and 'b'). For each key, it increments count.value by 1. So after one click, count is 2.
v-for with objects in Vue 3 is TRUE?Choose the correct statement about using v-for to iterate over objects in Vue 3.
Think about best practices for rendering lists in Vue.
Providing a unique :key is important for Vue to track elements efficiently and avoid rendering bugs. This applies to objects as well as arrays.