Iterating over objects in Vue
Objects are also iterated over using the v-for directive. Let's see how this is done. Let's say we have the following object:
data() {
return {
obj: {a: 1, b: 2, c: 3},
}
}
Let's iterate over this object and display its elements:
<template>
<p v-for="elem in obj">
{{ elem }}
</p>
</template>
Now let's output both the keys and the elements:
<template>
<p v-for="(elem, key) in obj">
{{ key }} {{ elem }}
</p>
</template>
Now let's also output the ordinal numbers of the elements in the object:
<template>
<p v-for="(elem, key, index) in obj">
{{ index }}
{{ key }}
{{ elem }}
</p>
</template>
The following object is given:
{
user1: '100$',
user2: '200$',
user3: '300$',
}
Use v-for to print the following:
<ul>
<li>100$</li>
<li>200$</li>
<li>300$</li>
</ul>
The following object is given:
{
user1: '100$',
user2: '200$',
user3: '300$',
}
Use v-for to print the following:
<ul>
<li>user1 - 100$</li>
<li>user2 - 200$</li>
<li>user3 - 300$</li>
</ul>
Rework the previous task so that at the end of each li there is also a serial number of the element in the object. Like this:
<ul>
<li>user1 - 100$ - 0</li>
<li>user2 - 200$ - 1</li>
<li>user3 - 300$ - 2</li>
</ul>
Rework the previous problem so that the numbers start with one instead of zero. Like this:
<ul>
<li>user1 - 100$ - 1</li>
<li>user2 - 200$ - 2</li>
<li>user3 - 300$ - 3</li>
</ul>