Home  >  Q&A  >  body text

javascript - How does vue's loop v-for loop through 2 items at a time?

This is how I implement it now, but I always feel like something is wrong

<ul v-for="(item,index) in items" v-if="index%2==0">
  <li>{{items[index].name}}</li>
  <li>{{items[index+1].name}}</li>
</ul>
给我你的怀抱给我你的怀抱2713 days ago818

reply all(8)I'll reply

  • 为情所困

    为情所困2017-05-16 13:22:30

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <p id="app">
            <input type="number" v-model.number="count" />
            <ol>
                <li v-for="g in groups">
                    <ol>
                        <li v-for="item in g">{{ item }}</li>
                    </ol>
                </li>
            </ol>
        </p>
    
        <script src="http://cdn.bootcss.com/vue/2.1.6/vue.min.js"></script>
    
        <script>
    
            function GroupByCount(items, count) {
                var len = items.length;
    
                var arr = [];
                var child = [];
    
                for (var i = 0; i < len; i++) {
                    child.push(items[i]);
    
                    if (child.length === count) {
                        arr.push(child);
                        child = [];
                    }
                }
    
                if (child.length > 0) {
                    arr.push(child);
                }
    
                return arr;
            }
    
            new Vue({
                el: '#app',
                data() {
                    return {
                        count: 2,
                        items: []
                    }
                },
                created() {
                    this.items = Array.apply(null, Array(20)).map(function (x, i) { return i; })
                },
                computed: {
                    groups() {
                        return GroupByCount(this.items, this.count)
                    }
                }
            })
        </script>
    </body>
    </html>

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:22:30

    <ul v-for="(item,index) in items">    
        <slot v-if="index<items.length&&index%2==0">
          <li >{{items[index].name}}</li>
          <li v-if="index<items.length-1">{{items[index+1].name}}</li>
        </slot>
    </ul>

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 13:22:30

    <ul v-for="i in (items.length / 2)">
      <li>{{items[(i - 1) * 2].name}}</li>
      <li>{{items[(i - 1) * 2 + 1].name}}</li>
    </ul>

    That’s probably what it means, but there are some details that you need to think about a little more, such as what to do when items.lengthis an odd number

    reply
    0
  • 某草草

    某草草2017-05-16 13:22:30

    index是从0开始的

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-16 13:22:30

    Why do I feel that there is no difference between writing it this way and looping it all? . .

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:22:30

    What are your specific needs? You can’t tell anything by looking at your code

    Is the following what you want?
    This is a calculated attribute, split the array into two arrays and put them into one array

    itemsComputed (){
        let arr = [];
        arr.push(this.items.filter((o,i)=>i%2===0));
        arr.push(this.items.filter((o,i)=>i%2===1));
        return arr;
    }
        <ul>
            <li v-for="item in itemsComputed[0]">
                ...
            </li>
        </ul>
        <ul>
            <li v-for="item in itemsComputed[1]">
                ...
            </li>
        </ul>

    reply
    0
  • 高洛峰

    高洛峰2017-05-16 13:22:30

    If you can use template syntax directly, you don’t need to do extra calculations:

    <template v-for="(item,index) in items">
        <ul v-if="index % 2 == 0">
            <li>{{items[index].name}}</li>
            <li v-if="index < items.length">{{items[index+1].name}}</li>
        </ul>
    </template>

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:22:30

    Uh? Is this for group display?

    <template>
        <ul v-for="(item,idx) in b">
          <li v-for="i in item">{{i}}</li>
        </ul>
    </template>
    
    <script>
    export default {
      data () {
        return {
          a: [1, 2, 3, 4, 5]
        }
      },
      computed: {
        b () {
          const b = []
          const a = this.a
          for (let i = 0, l = a.length; i < l;) {
            b.push([a[i++], a[i++]])
          }
          return b
        }
      }
    }
    </script>
    

    reply
    0
  • Cancelreply