Sep 10, 2025
Generator function to split array into chunks
export function* chunks<T>(arr: T[], n: number): Generator<T[]> { for (let i = 0; i < arr.length; i += n) { yield arr.slice(i, i + n); } } // Usage const numbers = [1, 2, 3, 4, 5, 6, 7]; // Split into chunks of 3 const result = [...chunks(numbers, 3)]; console.log(result); // [ // [1, 2, 3], // [4, 5, 6], // [7] // ]