Search code examples
vue.jsvuejs3vue-componentvue-composition-api

what does _ mean as first parameter in for-loop


referring to this link, and as i peruse and inspect the code posted below,attracted my attention the

v-for="(_, tab) in tabs"

as shown in the code below.

i would like to know what does _ mean as first parameter in a for-loop please

code:

<script setup>
import Home from './Home.vue'
import Posts from './Posts.vue'
import Archive from './Archive.vue'
import { ref } from 'vue'
 
const currentTab = ref('Posts')

const tabs = {
  Home,
  Posts,
  Archive
}
</script>

<template>
  <div class="demo">
    <button
       v-for="(_, tab) in tabs"
       :key="tab"
       :class="['tab-button', { active: currentTab === tab }]"
       @click="currentTab = tab"
     >
      {{ tab }}
    </button>
      <component :is="tabs[currentTab]" class="tab"></component>
  </div>
</template>

Solution

  • It's just convention for an unused variable.

    What you're really using is Vue's v-for with an Object

    v-for="(value, key) in collection"
    

    But in your implementation:

    • collection -> tabs
    • value -> _
    • key -> tab

    If it helps you understand it better, consider this

    <button
      v-for="tabName in Object.keys(tabs)"
      :key="tabName"
      :class="['tab-button', { active: currentTab === tabName }]"
      @click="currentTab = tabName"
    >
      {{ tabName }}
    </button>