假设有一个数组 arr,我们可以使用以下代码将其分成 n 行并放入表格中:

const arr = [1, 2, 3, 4, 5, 6]; // 假设有一个数组
const n = 3; // 假设要分成 3 行

const table = document.createElement('table'); // 创建一个表格元素

// 遍历数组并将其分成 n 行
for (let i = 0; i < arr.length; i += n) {
  const row = document.createElement('tr'); // 创建一个表格行元素

  // 将每个数据放入行中
  for (let j = i; j < i + n; j++) {
    if (j < arr.length) { // 避免越界
      const cell = document.createElement('td'); // 创建一个表格单元格元素
      cell.textContent = arr[j]; // 将数据放入单元格中
      row.appendChild(cell); // 将单元格放入行中
    }
  }

  table.appendChild(row); // 将行放入表格中
}

document.body.appendChild(table); // 将表格放入页面中

以上代码将数组 [1, 2, 3, 4, 5, 6] 分成 3 行放入表格中。你可以根据自己的需求修改数组和行数

js把map集合把n个数据分成n行放入表格里面怎么写

原文地址: https://www.cveoy.top/t/topic/frGt 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录