将数据按照几个为一组重新放置进数组

数据 :

      // this.queryDeviceList = [{
              //         deviceName: "灯光",
              //         flagName: "lamp",

              //         logoStyle: {
              //             background: ' url("/static/images/card/lamb_logo@2x.png") no-repeat',
              //             backgroundSize: '100% 100%',
              //         },

              //         value: [{
              //                 id: 94,
              //                 name: "灯光1",
              //                 lampState: ["关闭", "打开"],
              //             },
              //             {
              //                 id: 95,
              //                 name: "灯光2",
              //                 lampState: ["关闭", "打开"],
              //             },
              //         ],
              //     },
              //     {
              //         deviceName: "测试",
              //         flagName: "lamp",

              //         logoStyle: {
              //             background: ' url("/static/images/card/lamb_logo@2x.png") no-repeat',
              //             backgroundSize: '100% 100%',
              //         },
              //         value: [{
              //                 id: 94,
              //                 name: "灯光1",
              //                 lampState: ["关闭", "打开"],
              //             },
              //             {
              //                 id: 95,
              //                 name: "灯光2",
              //                 lampState: ["关闭", "打开"],
              //             },
              //         ],
              //     },
              //     {
              //         deviceName: "窗帘",
              //         flagName: "curtain",
              //         allControlFlag: false,
              //         logoStyle: {
              //             background: ' url("/static/images/card/curtain_logo@2x.png") no-repeat',
              //             backgroundSize: '100% 100%',
              //         },
              //         value: [{
              //                 id: 91,
              //                 name: "窗帘1",
              //                 currentState: ["关闭", "打开", "停止"],
              //             },
              //             {
              //                 id: 92,
              //                 name: "窗帘2",
              //                 currentState: ["关闭", "打开", "停止"],
              //             },
              //         ],
              //     },

              //     {
              //         deviceName: "空调",
              //         flagName: "air",

              //         logoStyle: {
              //             background: ' url("/static/images/card/airconditioner_logo@2x.png") no-repeat',
              //             backgroundSize: '100% 100%',
              //         },
              //         value: [{
              //             id: 106,
              //             name: "空调",
              //             airState: ["打开空调", "关闭空调"],
              //             airWindVelocity: [
              //                 "风速一档",
              //                 "风速二档",
              //                 "风速三档",
              //                 "风速自动",
              //             ],
              //             airmodel: ["制热模式", "制冷模式", "送风模式", "自动模式"],
              //             setAirWind: {
              //                 base: "设置温度",
              //                 choose: [
              //                     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
              //                     31, 32,
              //                 ],
              //             },
              //         }, ],
              //     },
              //     {
              //         deviceName: "大屏",
              //         flagName: "screen",

              //         logoStyle: {
              //             background: 'url("/static/images/card/screen_logo@2x.png") no-repeat',
              //             backgroundSize: '100% 100%',
              //         },
              //         value: [{
              //                 id: 109,
              //                 name: "大屏1",
              //                 screenState: ["打开", "关闭"],
              //             },
              //             {
              //                 id: 110,
              //                 name: "大屏2",
              //                 screenState: ["打开", "关闭"],
              //             },
              //         ],
              //     },
              // ];

例:三个为一组

let num = Math.ceil(deviceList.length / 3);

先向上取整 取余 

当数量超过3个时会有余数

      当余数大于等于2时将剩下的放入新的数组中


                    if (num >= 2) {
                        for (let i = 0; i < num; i++) {
                            if (i === num - 1) {
                                this.tabList.push(

//有超过三个的倍数,但是这些组最后的没有满足3个 ,  取从3倍数开始一直到结尾
                                    deviceList.slice(i * 3, deviceList.length)
                                );
                                console.log(this.tabList);
                                return;
                            } else {


                                this.tabList.push(
                                    deviceList.slice(i * 3, (i + 1) * 3)
                                );
                                console.log(this.tabList);
                            }
                        }
                    } else {

// 全部数量未满三个
                        this.tabList.push(deviceList);
                    }

以上无用直接用下面这个

let arr = [];
        let num = 4 // 填写需要分割成几个一组的数量
          if (this.imgData.length) {
              for (let i = 0; i < this.imgData.length; i += num) {
                  arr.push(this.imgData.slice(i, i + num));
              }
          }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值