Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
difference between v-for structures

Could someone explain what is the difference between those 2 v-for structures:

<li v-for="item in items" :key="item">
</li>

and

<li v-for="(item, i) in items" :key="i">
</li>

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

In the first case, the v-for iterates over all elements of items. It assigns the :key to the item itself. This is not ideal if you have duplicate elements in items, because each :key should be unique.

In the second case, v-for also iterates over all elements of items, but it introduces another variable named i, which represents the numerical index of the item in items. This index is then assigned to :key. This is better, because the indexes can't be duplicate.

over 4 years ago · Santiago Trujillo Report

0

As the purpose of :key attribute is to give a hint for Vue virtual DOM algorithm about change detection happen. Essentially, it helps Vue identify what's changed and what hasn't.

To understand it better I am adding the uses of both the scenarios :

  • :key="item" - If item is having unique value. So that each iterated element will get assigned with unique key.

    Demo screenshot

  • :key="index" - If item is not having unique value. Hence, to assigned unique key to each iterated element we are using index.

    Demo screenshot

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!