How to get the v-for index in Vue.js?

Sometimes, we want to get the v-for index in Vue.js.

In this article, we’ll look at how to get the v-for index in Vue.js.

How to get the v-for index in Vue.js?

To get the v-for index in Vue.js, we can get it from the 2nd parameter.

For instance, we write

<template>
  <div id="app">
    <div v-for="(item, index) in items" :key="item.name">
      {{ index }}: {{ item.name }}
    </div>
  </div>
</template>

<script>
//...
export default {
  //...
  data() {
    return {
      items: [{ name: "a" }, { name: "b" }],
    };
  },
  //...
};
</script>

to render the items array with v-for.

We get the index of each item from index.

Conclusion

To get the v-for index in Vue.js, we can get it from the 2nd parameter.