[Vuejs]-Loop variables in Vue.js similar to AngularJS

5πŸ‘

βœ…

It works for me πŸ™‚

<div v-for="(item, index) in items">
    <span  v-if="index === (items.length-1)">This is the last loop element</span>
</div>
πŸ‘€Nisha

1πŸ‘

you can also put the logic in a computed property :

  <div v-for="(value, key) in myObj">
    <span v-if="key === last">This is the last loop element : {{ value }}</span>
  </div>

  //...
  computed: {
    last() {
      let keys = Object.keys(this.myObj)
      return keys.slice(-1)[0]
    }
  }

fiddle here

πŸ‘€Sovalina

0πŸ‘

There’s no equivalent for this in Vue, no.

πŸ‘€Linus Borg

0πŸ‘

You can do this instead, check for index if it is last

  <div v-for="(value, key, index) in myObj">
     <div v-if="index === Object.keys(myObj).length-1"> my content</div>
  </div>
πŸ‘€sridhar..

0πŸ‘

You can try this

<div v-repeat="myObj">
 <span v-if="$index === (myObj.length-1)">Shows if it is the last loop element</span>
</div>

0πŸ‘

Based on @Nisha answer I have been doing this:

<template v-for="(obj, index) in originalData">
    <template v-if="index == 0 || (index >= 1 && obj.valueA !== originalData[index - 1].valueA)">
        {{ obj.valueA }}
    </template>
</template>

I want the loop to always happen the first time, but to conditionally check a value for every loop afterwards. I am using <template> tags to avoid outputting unnecessary markup. In my example originalData is an array of objects.

πŸ‘€freeMagee

Leave a comment