Chapter 7: Vue v-for

Think of v-for as Vue’s built-in forEach that magically turns your data array (or object) into repeated HTML elements — and keeps them in sync when the data changes (add/remove/reorder items).

Official Vue 3 docs (vuejs.org/guide/essentials/list.html) say:

We can use the v-for directive to render a list of items based on an array. The v-for directive requires a special syntax in the form of item in items, where items is the source data array and item is an alias for the array element being iterated on.

Let’s break it down step by step with real, runnable examples.

1. Basic Syntax – Arrays (most common case)

vue

→ Vue creates one <li> for each item → Change fruits.value.push(‘Jackfruit’) → new <li> appears instantly

Important rule #1 (never forget this!) Always add :key attribute with a unique value per item.

Why? Vue uses the :key to track identity of each element when the list changes (add/delete/reorder). Without it → bugs like wrong animations, lost input focus, inefficient updates.

Bad (don’t do this in 2026):

HTML

Good (always):

HTML

2. With Index – When you need position

vue

Or more useful:

vue

Note: Use :key=”user.id” (unique ID) instead of index whenever possible — index as key is only okay for static lists that never reorder/add/delete.

3. Looping over Objects

vue

→ You get three arguments: value, key, index → Use <template v-for> so no extra <div> wrapper

4. v-for with Components (very common in real apps)

vue

Child (ProductCard.vue) receives prop and works normally.

5. v-for + v-if / v-else (careful combination!)

Never put v-if and v-for on the same element — Vue 3 evaluates v-if first → can cause bugs or unnecessary renders.

Wrong (avoid):

HTML

Correct ways:

A. Use computed (best performance)

JavaScript
HTML

B. Wrap with <template>

HTML

6. Updating Lists Reactively – Best Practices 2026

JavaScript

Quick Cheat Sheet Table

Syntax Use Case Must-have Notes / 2026 Tip
v-for=”item in items” Simple array :key=”item.id” Use unique ID
v-for=”(item, index) in items” Need position / numbering :key (prefer ID) Index as key only static lists
v-for=”(val, key) in object” Loop object properties :key=”key” Use <template> wrapper
v-for=”n in 10″ Repeat N times (rare) :key=”n” Like range(1,11)
v-for + component List of cards/users/posts :key + props Most common real usage
v-for + v-if on same tag Never — use computed or <template>

Pro Tips from Real 2026 Projects

  • Always :key with unique stable value (database ID > UUID > index)
  • Prefer computed for filtering/sorting → keeps template clean
  • For very long lists (1000+ items) → look into v-memo (Vue 3.2+) or virtual scrolling libraries (vue-virtual-scroller, TanStack Virtual)
  • Use <TransitionGroup> + v-for for animated list adds/removals
vue

Practice challenge: Build a dynamic todo list with:

  • Add todo (push)
  • Toggle done
  • Delete (splice)
  • Filter “active” / “completed” with computed + v-for

Any part confusing? Want full todo app code? Difference between in vs of? (both work, in is official) Or how to handle pagination / infinite scroll with v-for?

Just say — we’ll build it together step by step 🚀

Happy looping from Hyderabad! 🌶️💚

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *