1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| function pages(arr, len) { const pages = []; arr.forEach((item, index) => { const page = Math.floor(index / len); if (!pages[page]) { pages[page] = []; } pages[page].push(item); }); return pages; }
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; console.log(pages(arr, 3)); console.log(pages(arr, 8));
|