Home  >  Q&A  >  body text

javascript - When traversing Vue's v-for, can the index value be controlled at a maximum value and output back and forth?

Regarding vue's v-for traversal, can the index index value be controlled to cycle back and forth at a maximum value?


Attach the jsfiddle code address, click to go to jsfiddle preview code>>

Code:

<script type="text/javascript">
var vm=new Vue({
    el:'.list',
    data:{
        list:['a','b','c','d','e','f','g']
    },
});
</script>
<p class="list">
<ul>
  <li v-for="(item,index) in list">
    {{index}}<br />{{item}}
  </li>
</ul>
</p>

For example, if you want the maximum index value of the loop to be 2, the expected result is as follows:

<p clss="list">
    0
    a
</p>
<p clss="list">
    1
    b
</p>
<p clss="list">
    2
    c
</p>
<p clss="list">
    0
    d
</p>
<p clss="list">
    1
    e
</p>
<p clss="list">
    2
    f
</p>
<p clss="list">
    0
    g
</p>
typechotypecho2663 days ago1179

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-07-05 11:04:34

    <p class="list">
    <ul>
      <li v-for="(item,index) in list">
        {{index%3}}<br />{{item}}
      </li>
    </ul>
    </p>
    

    Use % to find the remainder?

    Update:
    =.= changed to 3 to prevent misleading. The point is %

    reply
    0
  • PHP中文网

    PHP中文网2017-07-05 11:04:34

    It should be index%3

    reply
    0
  • 三叔

    三叔2017-07-05 11:04:34

    I think we can use filter to process the index

    reply
    0
  • Cancelreply