考虑以下JavaScript数组:
const allRows = [
{id: 1, name: Name 1},
{id: 2, name: Name 1},
{id: 3, name: Name 1},
{id: 4, name: Name 1},
{id: 5, name: Name 1},
{id: 6, name: Name 1},
{id: 7, name: Name 1},
{id: 8, name: Name 1},
{id: 9, name: Name 1},
{id: 10, name: Name 1},
{id: 11, name: Name 1},
{id: 12, name: Name 1},
{id: 13, name: Name 1},
{id: 14, name: Name 1},
{id: 15, name: Name 1},
{id: 16, name: Name 1},
{id: 17, name: Name 1},
{id: 18, name: Name 1},
{id: 19, name: Name 1},
{id: 20, name: Name 1},
{id: 21, name: Name 1},
{id: 22, name: Name 1},
{id: 23, name: Name 1},
{id: 24, name: Name 1},
{id: 25, name: Name 1},
{id: 26, name: Name 1},
{id: 27, name: Name 1},
{id: 28, name: Name 1},
{id: 29, name: Name 1},
{id: 30, name: Name 1},
];
//想要遍历数组并将其转换为数组,如下所示
let rowsPaginated = [
[
{id: 1, name: Name 1},
{id: 2, name: Name 1},
{id: 3, name: Name 1},
{id: 4, name: Name 1},
{id: 5, name: Name 1},
{id: 6, name: Name 1},
{id: 7, name: Name 1},
{id: 8, name: Name 1},
{id: 9, name: Name 1},
{id: 10, name: Name 1}
],
[
{id: 11, name: Name 1},
{id: 12, name: Name 1},
{id: 13, name: Name 1},
{id: 14, name: Name 1},
{id: 15, name: Name 1},
{id: 16, name: Name 1},
{id: 17, name: Name 1},
{id: 18, name: Name 1},
{id: 19, name: Name 1},
{id: 20, name: Name 1}
],
[
{id: 21, name: Name 1},
{id: 22, name: Name 1},
{id: 23, name: Name 1},
{id: 24, name: Name 1},
{id: 25, name: Name 1},
{id: 26, name: Name 1},
{id: 27, name: Name 1},
{id: 28, name: Name 1},
{id: 29, name: Name 1},
{id: 30, name: Name 1}
]
];
我正在考虑以这种方式转换 -
for (let i = 0; i < allRows.length; i++) {
console.log(allRows[i]);
}
但我认为这不是一个好方法。 循环遍历allRows数组并转换为rowsPaginated数组的最佳方法是什么?
答案 0 :(得分:2)
使用数组slice()
,其大小要从数组中删除。请尝试以下方法:
const allRows = [
{id: 1, name: 'Name 1'},
{id: 2, name: 'Name 1'},
{id: 3, name: 'Name 1'},
{id: 4, name: 'Name 1'},
{id: 5, name: 'Name 1'},
{id: 6, name: 'Name 1'},
{id: 7, name: 'Name 1'},
{id: 8, name: 'Name 1'},
{id: 9, name: 'Name 1'},
{id: 10, name: 'Name 1'},
{id: 11, name: 'Name 1'},
{id: 12, name: 'Name 1'},
{id: 13, name: 'Name 1'},
{id: 14, name: 'Name 1'},
{id: 15, name: 'Name 1'},
{id: 16, name: 'Name 1'},
{id: 17, name: 'Name 1'},
{id: 18, name: 'Name 1'},
{id: 19, name: 'Name 1'},
{id: 20, name: 'Name 1'},
{id: 21, name: 'Name 1'},
{id: 22, name: 'Name 1'},
{id: 23, name: 'Name 1'},
{id: 24, name: 'Name 1'},
{id: 25, name: 'Name 1'},
{id: 26, name: 'Name 1'},
{id: 27, name: 'Name 1'},
{id: 28, name: 'Name 1'},
{id: 29, name: 'Name 1'},
{id: 30, name: 'Name 1'},
];
let rowsPaginated = [];
var i, j, size = 10;
for (i = 0, j = allRows.length; i < j; i += size) {
rowsPaginated.push(allRows.slice(i, i+size));
}
console.log(rowsPaginated);
&#13;
答案 1 :(得分:0)
尝试以下代码,我纠正了你的数组错误:
const allRows = [
{id: 1, name: "Name1"},
{id: 2, name: "Name1"},
{id: 3, name: "Name1"},
{id: 4, name: "Name1"},
{id: 5, name: "Name1"},
{id: 6, name: "Name1"},
{id: 7, name: "Name1"},
{id: 8, name: "Name1"},
{id: 9, name: "Name1"},
{id: 10, name: "Name1"},
{id: 11, name: "Name1"},
{id: 12, name: "Name1"},
{id: 13, name: "Name1"},
{id: 14, name: "Name1"},
{id: 15, name: "Name1"},
{id: 16, name: "Name1"},
{id: 17, name: "Name1"},
{id: 18, name: "Name1"},
{id: 19, name: "Name1"},
{id: 20, name: "Name1"},
{id: 21, name: "Name1"},
{id: 22, name: "Name1"},
{id: 23, name: "Name1"},
{id: 24, name: "Name1"},
{id: 25, name: "Name1"},
{id: 26, name: "Name1"},
{id: 27, name: "Name1"},
{id: 28, name: "Name1"},
{id: 29, name: "Name1"},
{id: 30, name: "Name1"}
];
let rowsPaginated = [];
let j = 0;
for (let i = 0, j = allRows.length; i < j; i += 10) {
rowsPaginated.push(allRows.slice(i, i+10));
}
console.log(rowsPaginated);