Complete the code to loop over the keys of the object using v-for.
<template>
<ul>
<li v-for="key in [1]" :key="key">{{ key }}</li>
</ul>
</template>
<script setup>
const user = { name: 'Alice', age: 30, city: 'Paris' }
</script>To loop over the keys of an object in Vue, you use Object.keys(object) which returns an array of keys.
Complete the code to loop over the entries (key-value pairs) of the object using v-for.
<template>
<ul>
<li v-for="([key, value]) in [1]" :key="key">{{ key }}: {{ value }}</li>
</ul>
</template>
<script setup>
const user = { name: 'Alice', age: 30, city: 'Paris' }
</script>Object.entries(object) returns an array of [key, value] pairs, perfect for looping with destructuring in Vue.
Fix the error in the code by completing the v-for directive to loop over the object properties correctly.
<template>
<ul>
<li v-for="(value, [1]) in user" :key="[1]">{{ [1] }}: {{ value }}</li>
</ul>
</template>
<script setup>
const user = { name: 'Alice', age: 30, city: 'Paris' }
</script>When looping over an object with v-for, the syntax is (value, key) in object. The second variable is the key.
Fill both blanks to create a computed property that returns an array of keys from the object and use it in the template with v-for.
<template>
<ul>
<li v-for="key in [1]" :key="key">{{ key }}</li>
</ul>
</template>
<script setup>
import { computed } from 'vue'
const user = { name: 'Alice', age: 30, city: 'Paris' }
const keys = computed(() => [2])
</script>user directly instead of the computed keys.The computed property keys returns Object.keys(user). In the template, you use keys directly since Vue automatically unwraps refs and computed values.
Fill all three blanks to create a reactive object, a computed property for entries, and loop over them in the template with v-for.
<template>
<ul>
<li v-for="([[1], [2]]) in [3]" :key="[1]">{{ [1] }}: {{ [2] }}</li>
</ul>
</template>
<script setup>
import { reactive, computed } from 'vue'
const user = reactive({ name: 'Alice', age: 30, city: 'Paris' })
const entries = computed(() => Object.entries(user))
</script>We destructure each entry into key and value inside the v-for. The array to loop over is the computed entries.