Render lists of data in Vue with v-for

Rory Smith
InstructorRory Smith

Share this video with your friends

Send Tweet
Published 6 years ago
Updated 4 years ago

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.

[00:00] 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.

[00:14] 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.

[00:36] 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.

[00:50] 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.

[01:12] 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.

[01:23] 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.