Categories
Vue Answers

How to Get the Index of the Item Being Rendered with v-for in Vue.js?

Spread the love

Sometimes, we want to get the index of the item being rendered with v-for in Vue.js.

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

Get the Index of the Item Being Rendered with v-for in Vue.js

We can get the index of the item being rendered with v-for in Vue.js from the 2nd item in the parentheses in v-for .

For instance, we can write:

<template>
  <div id="app">
    <div v-for="(item, index) in items" :key="item.name">
      {{ index }}: {{ item.name }}
    </div>
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      items: [{ name: "a" }, { name: "b" }],
    };
  },
};
</script>

to get the item index from the index variable.

Conclusion

We can get the index of the item being rendered with v-for in Vue.js from the 2nd item in the parentheses in v-for .

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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