需求背景:
有一个不定行的表格,分为N个表格,第一个表格2行,后续表格都是3行
// 函数切割
function splitArrayIntoTables(rows) {
const tables = [];
let remainingRows = rows;
// Step 1: Handle the first table (always 2 rows)
if (remainingRows.length >= 2) {
tables.push(remainingRows.slice(0, 2));
remainingRows = remainingRows.slice(2);
} else if (remainingRows.length > 0) {
tables.push(remainingRows);
return tables; // Only one table if less than 2 rows
}
// Step 2: Handle the second table (always 3 rows or remaining rows)
if (remainingRows.length >= 3) {
tables.push(remainingRows.slice(0, 3));
remainingRows = remainingRows.slice(3);
} else if (remainingRows.length > 0) {
tables.push(remainingRows);
return tables; // Only two tables if less than 12 rows
}
// Step 3: Handle subsequent tables (each with up to 10 rows)
while (remainingRows.length > 0) {
tables.push(remainingRows.slice(0, 3));
remainingRows = remainingRows.slice(3);
}
return tables;
}