Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 986 of the free egghead.io lessons, plus get Vue.js content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Render lists of data in Vue with v-for

1:40 Vue.js lesson by

Let's write some example data and render it to our page with v-for, and then see how we can use the same directive to render the same component multiple times.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Let's write some example data and render it to our page with v-for, and then see how we can use the same directive to render the same component multiple times.

In our data, let's set up a people array. Then, let's output all of the people from within our template. We'll make an unordered list and each name is going to be a list item.

We can loop over the people array using the v4 directive and a person is going to represent each item of the people array. In this case, person is an alias for the item being iterated on and within the list text. We can use templates and text to refer to the person and here is the result.

We can even use v4 on objects. Let's specify a cats stats object, which will share a name, a breed, and a favorite thing. Then, we can iterate through the properties of our object.

We can specify the value and the key and then simply refer to them in the same way that we did with our people array. Here's the result. In either case of an array or an object, we can also pass in the specific index, which will be the index of the value being iterated on. Let's test that. Here are our indexes.

We can even use v4 to tell vue how many time to render something by passing it an integer. Let's set up an amount of horizontal rules value in our data.

In this case, N is going to represent the nth time that this div is being rendered, we'll put in the current number next to our horizontal rule, and we can see the amount of horizontal rules that we specify in our data.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?