【问题标题】:Split array into differently N sized chunks将数组拆分为不同大小的 N 块
【发布时间】:2020-09-16 01:23:13
【问题描述】:

我想知道这在 VueJS 中是否可行

我有一个包含 N 项的数组,我想将其拆分为不同长度的数组,最终输出类似这样的内容(注意第一行有两列,第二行有 3 列)

Array = [{"text": "hello"},{"text": "hello1"},{"text": "hello2"},{"text": "hello3"},{"text": "hello4"},{"text": "hello5"},{"text": "hello6"},{"text": "hello7"},{"text": "hello8"}]
<div class="row">
    <div class="col-1-of-2">
       hello
    </div>
    <div class="col-1-of-2">
        hello1
    </div>
</div>
<div class="row">
    <div class="col-1-of-3">
       hello2
    </div>
    <div class="col-1-of-3">
        hello3
    </div>
    <div class="col-1-of-3">
        hello4
    </div>
</div>

然后重复

我认为您需要对数组进行分块,但我看到的示例假定 N 长度是固定的

【问题讨论】:

    标签: arrays vue.js vuejs2


    【解决方案1】:

    最常见的处理方法是使用计算。

    取出数组,并根据需要嵌套(不确定你的规则是 2 然后 3)

    然后在模板中使用嵌套循环来显示数据

    new Vue({
      el: "#app",
      data() {
        return {
          split: "2,3",
          items: [{"text": "hello"},{"text": "hello1"},{"text": "hello2"},{"text": "hello3"},{"text": "hello4"},{"text": "hello5"},{"text": "hello6"},{"text": "hello7"},{"text": "hello8"},{"text": "hello9"}]
        }
      },
      computed: {
        rows() {
          const { split, items } = this;
    			let ret = [];
          let si = 0;
          let ci = 0;
          let splitArr = split.split(',');
          this.items.forEach(item => {
            if(!ret[si]){ret.push([])}
            ret[si][ci] = item
            ci = (ci + 1)
            if (ci >= splitArr[si % splitArr.length]) {
            	ci = 0;
              si++;
            }
            
          })
          return ret
        }
      },
    })
    .row{
      display: block;
    }
    .col{
      box-sizing: border-box;
      padding: 10px;
      margin: 2px;
      border: 1px solid;
      display: inline-block;
    }
    .col-1-of-1{
      width: calc(100% - 4px);
    }
    .col-1-of-2{
      width: calc(50% - 4px);
    }
    .col-1-of-3{
      width: calc(33.3% - 4px);
    }
    .col-1-of-4{
      width: calc(25% - 4px);
    }
    .col-1-of-5{
      width: calc(20% - 4px);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
    <div id="app">
      <div>
        SPLIT: <input v-model="split">
      </div>
      <div class="row" v-for="row in rows">
        <div :class="`col col-1-of-${row.length}`" v-for="col in row">
          {{col.text}}
        </div>
      </div>
    </div>

    【讨论】:

    • 没问题。我试图弄清楚如何首先划分数组:D
    • 你能详细说明一下逻辑是什么,分离是如何发生的吗?
    • 更新代码以根据文本输入动态更改
    【解决方案2】:

    你可以这样做:

    new Vue({
      el: "#app",
      data() {
        return {
          items: [{"text": "hello"},{"text": "hello1"},{"text": "hello2"},{"text": "hello3"},{"text": "hello4"},{"text": "hello5"},{"text": "hello6"},{"text": "hello7"},{"text": "hello8"}],
          size: 2
        }
      },
      computed: {
        rows() {
          let _newContent = [];
    
          while(this.items.length > 0) {
            _newContent.push(this.items.splice(0, this.size));
          }
    
          return _newContent;
        }
      },
    })
    

    【讨论】:

    • 我想你可能错过了我想要不同 N 大小的事实,所以 2 然后 3 然后 2 然后 3 等等,而不仅仅是 2,2,2,2,2
    • @user5067291,是的,我检查过了,但我回答说,如果他/她想动态改变块大小的形成
    • @user5067291 我没有提供确切的代码,我只想给他一个基本概念,即块任意数量的 N 大小数组就是这样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    相关资源
    最近更新 更多