let array = [
{ name: 'Alex', year: 21 }, { name: 'Vasya', year: 14 } ....
]
let array = [
[
{ name: 'Alex', year: 21 }, { name: 'Vasya', year: 14 }
],
[
......
]
]
const chunked = (data, chunkSize) =>
Array.prototype.reduce.call(
data,
(acc, n, i) => (
i = i / chunkSize | 0,
(acc[i] = acc[i] || []).push(n),
acc
),
[]
);
console.log(chunked([...Array(10).keys()], 3));
console.log(chunked('ABCDEFG', 2));
console.log(chunked(document.querySelectorAll('img'), 5));
const array = [
{ name: 'Alex1', year: 13 },
{ name: 'Alex2', year: 14 },
{ name: 'Alex3', year: 15 },
{ name: 'Alex4', year: 16 },
{ name: 'Alex5', year: 17 }
];
const result = [];
let chunkSize = 2;
for (let i=0; i < array.length; i += chunkSize) {
const chunk = array.slice(i, i + chunkSize);
result.push(chunk);
}
console.log(result);