Start range in v-for="n in 10" from zero
Asked Answered
H

3

45

I want to start the range from 0 instead of 1 in v-for="n in 10" which results in 1 2 3 .... 10 Is there a way to do it in Vuejs?

Handsaw answered 3/8, 2017 at 7:50 Comment(2)
I'm not sure if this can work to be honest, because 10 isn't an iterable... – Sextuplicate
@Sandrooco It will work in Vue. See dfsq's answer (or the docs: vuejs.org/v2/api/#v-for) – Rood
S
101

You can use index (i) instead of value (n), it will start with 0:

<div v-for="(n, i) in 10">{{ i }}</div>

Output:

0
1
2
...
Springtail answered 3/8, 2017 at 7:58 Comment(2)
While this works, I don't know if I can find this in documentation anywhere! Nice πŸ‘ – Ricotta
@Ricotta it's said in docs: vuejs.org/v2/guide/list.html "v-for also supports an optional second argument for the index of the current item." – Springtail
T
6

You can also just subtract a value from your integer

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

This way you can have numbers with a negative value

Ten answered 11/12, 2019 at 22:22 Comment(0)
R
-10

Just use v-for="i in 10" and you got values 1...10. Also you should use use the key prop like below.

<div v-for="i in 10" :key="i">{{ i }}</div>

Output

1
2
3
...
Rajasthani answered 5/4, 2019 at 11:9 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.