Render lists of data in Vue with v-for

Rory Smith
InstructorRory Smith
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 5 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.

egghead
egghead
~ 4 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today