I build a function to put array into chunk arrays like:
function conditionalChunk(array, size, rules = {}) {
let copy = [...array],
output = [],
i = 0;
while (copy.length)
output.push( copy.splice(0, rules[i++] ?? size) )
return output
}
const input = [[1,2,3],[4,5,6,7]],
output = conditionalChunk(input.flat(), 3, { 0: 2, 1: 2 });
// OUTPUT: [[1,2],[3,4],[5,6,7]]
and the chunk is actually pages in my case but I need some more details to put there so now I have:
[{'page_layout': 1, 'theme': 0, 'items', [1,2,3]}, {'page_layout': 5, 'theme': 1, 'items', [4,5,6,7]}]
How can I isolate items to build chunk arrays like in the case above without objects...
I was tried: output = conditionalChunk(input.flat().items, 3, { 0: 2, 1: 2 }); but that is not the solution and I got error" Uncaught TypeError: array is not iterable
input.flat().items->input.flaMapt(x => x.items)itemsas a flat array. If you don't want to do that, there is probably no reason to flatten anything. You'd need to explain what the goal is. What do you want to go over and how? What is the expected output?