表格切割

需求背景:

有一个不定行的表格,分为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;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容