Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/v-for-range-starts-at-one.md
1---2title: v-for Range Iteration Starts at 1, Not 03impact: LOW4impactDescription: v-for with a number range starts at 1, unlike JavaScript arrays which start at 05type: gotcha6tags: [vue3, v-for, list-rendering, range]7---89# v-for Range Iteration Starts at 1, Not 01011**Impact: LOW** - When using `v-for` with a number (range iteration), the iteration starts at `1`, not `0`. This differs from typical JavaScript behavior where arrays are 0-indexed.1213This gotcha commonly causes off-by-one errors when the generated numbers are used for calculations or array indexing.1415## Task Checklist1617- [ ] Remember `v-for="n in 10"` produces 1 through 10, not 0 through 918- [ ] When using range values for array indexing, subtract 1: `items[n - 1]`19- [ ] Consider creating a computed array if you need 0-based indices2021**Incorrect Assumption:**22```html23<!-- Developer expects 0-9, but gets 1-10 -->24<span v-for="n in 10">{{ n }}</span>25<!-- Output: 1 2 3 4 5 6 7 8 9 10 -->2627<!-- WRONG: Off-by-one error when used for array access -->28<li v-for="n in items.length" :key="n">29{{ items[n].name }} <!-- Error! items[10] is undefined when length is 10 -->30</li>31```3233**Correct:**34```html35<!-- Correct understanding: 1-based range -->36<span v-for="n in 10" :key="n">{{ n }}</span>37<!-- Output: 1 2 3 4 5 6 7 8 9 10 -->3839<!-- CORRECT: Adjust index for array access -->40<li v-for="n in items.length" :key="n">41{{ items[n - 1].name }} <!-- n-1 converts to 0-based -->42</li>4344<!-- BETTER: Just iterate the array directly -->45<li v-for="(item, index) in items" :key="item.id">46{{ index + 1 }}. {{ item.name }} <!-- index is 0-based, add 1 for display -->47</li>48```4950```html51<!-- Range for repeating elements (no array involved) -->52<div v-for="n in 3" :key="n" class="skeleton-row">53Loading placeholder {{ n }} of 3...54</div>55<!-- Output: Loading placeholder 1 of 3, 2 of 3, 3 of 3 -->56```5758## When Range Iteration Is Useful5960- Rendering a fixed number of placeholder/skeleton elements61- Creating pagination buttons: `v-for="page in totalPages"`62- Generating star ratings: `v-for="star in 5"`63- Repeating template structures a set number of times6465## Reference66- [Vue.js List Rendering - v-for with a Range](https://vuejs.org/guide/essentials/list.html#v-for-with-a-range)67