数据 :
// 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));
}
}