265

How can I repeat a loop via v-for X (e.g. 10) times?

<!-- want to repeat this (e.g.) 10 times -->

<ul>
  <li v-for="item in shoppingItems">
    {{ item.name }} - {{ item.price }}
  </li>
</ul>

The documentation shows:

<ul>
  <li v-for="item in 10">{{ item }}</li>
</ul>

<!-- or -->

<li v-for="n in 10">{{ n }} </li>

<!-- this doesn't work -->

<li v-for="item in 10">{{ item.price }}</li>

but from where does vue know the source of the objects? If I render it like the doc says, I get the number of items and items, but without content.

kissu
  • 40,416
  • 14
  • 65
  • 133
MikeCaine
  • 3,381
  • 2
  • 11
  • 10
  • 1
    Why wouldn't you just iterate shoppingItems? – Bert Jun 18 '17 at 17:51
  • 3
    Hy Bert - I only want to know how to give a v-for (the simplest way) a (x) number of repeats and the example in the doc do not work...... .... in pure js + ajax its so simple return < 9 or or or, but this dont work in the method... – MikeCaine Jun 18 '17 at 18:35
  • 1
    There are two distinct modes to `v-for`; when it is called against an integer, and when it is called against an array. Mixing those modes and trying to use an integer to control access to content of an array is likely to cause problems. If you need a subset of the array you can filter it. – Jason Aller Mar 02 '18 at 19:42
  • You MUST to iterate what you desire to show, if shoppingItems collection doesnt fit to you because of their length (because I'm assuming that as the problem) but it fits you because of their content, you MUST then to create a computed property returning a new array slicing the original array with your desired length...and then iterate that computed array, I find this is the proper way to do it – elverde Aug 10 '22 at 08:42

15 Answers15

374

You can use an index in a range and then access the array via its index:

<ul>
  <li v-for="index in 10" :key="index">
    {{ shoppingItems[index].name }} - {{ shoppingItems[index].price }}
  </li>
</ul>

Note that this is 1-indexed: in the first iteration, index is 1, and in the second iteration, index is 2, and so forth.

You can also check the Official Documentation for more information.

kissu
  • 40,416
  • 14
  • 65
  • 133
Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
  • 6
    It works ::) I have changed it slightly to
  • {{ shoppingItems.price }}
  • ==== MNY THXXXX Coder*****! ==== – MikeCaine Jun 18 '17 at 18:19
  • 4
    How to make that 10 a variable?, form the data of the component? – alvaro Mar 26 '19 at 13:20
  • 53
    Why this is 1-indexed is completely beyond me. `{{i}} ` => `1 2 3 4 5`. Use `{{i}} ` to get 0-indexing. – ggorlen Aug 28 '19 at 00:04
  • 3
    I noticed that you should write shoppingItems[index -1].name to iterate over entire array – peschanko Oct 23 '19 at 13:27
  • 7
    @ggorlen the `v-for` directive is different from the standard JS for loop. The first argument is an alias for the array element (not the index) in the standard use case. The index is given as a second element so `(e, i)` returns the element and its index. When using `v-for="n in 10"`, `n` is the "element" which, I guess, is index+1. So you're solution is correct: to get the index use `v-for="(n, i) in 10"` where `n` is 1-indexed and `i` is 0-indexed. Details about the directive are at https://vuejs.org/v2/guide/list.html – phip Jan 05 '21 at 23:50
  • 1
    Small note but here, `index` is the actual element and not an index. If that would be the case, using a (real) index would be a quite bad practice overall. – kissu Oct 24 '22 at 09:55