Render Arrays of Data as Lists with the Vue v-for directive

Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated a year ago

We'll see how we can use the v-for directive to iterate over an array or object. We can define a temporary variable to reference the currently targeted element and render content based on this.

Instructor: [0:00] Let's add an array called details that has strings describing our comedy event. We can see it in our v tools, the details prop in our data object. We're going to create an unordered list just below where we have our ticket.

[0:14] In here, we want a list item for each of the details that we've just talked about. We want each of these to be rendered as a list item here. The way we do that is by using the v directive, v-for, v-for takes a string like this, detail in details.

[0:32] The details matches exactly in the [inaudible] array. The detail over here can be whatever you'd like it to be. I could just call it "thing" in details if I wanted to. By convention, we have plural and singular.

[0:45] Thing here is looped over four times, details, details, details, details, but in fact, I want that detail to be rendered instead. What you should also do is provide a key. In this case, I'm going to parse in the detail itself as the key.

[1:00] There are a few other syntaxes that are available to us. Instead of detail, we could have, detail and index, in details, which provides us with this extra value index. We could have something like, index, and then detail, if you'd rather. If it reads better for your context, you could use alt instead of in. There's no difference in functionality there, maybe that it reads better in the particular context.

[1:25] If instead of an array you had an object, let's put an object of some information about me, then you can see that that index, we've got detail, is actually the value, and index is actually the key. We can do the key and value. In this way, we can reference different data and present it in different ways.

[1:45] We have full access to the parent scope. If we wanted to reference how many tickets are left. We can do that freely. Lastly, if it is an object we're iterating over, we can get access to the index in that object by parsing as a third argument to our v-for string, and then reference that in our iteration.

egghead
egghead
~ 3 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