While I'm sure we could do this with Ramda, the first thing that comes to mind is a simple recursive call:
const groups = ([n, ...ns], xs) =>
n == undefined || xs .length == 0
? []
: [xs .slice (0, n), ... groups (ns, xs .slice (n))]
const All = {id : [ "a", "b", "c", "d", "e"], count : [1, 2, 2]}
console .log (groups (All.count, All.id))
We simply take the first group off the top and then recursively call with the remainder of the array and a smaller list of counts.
I originally wrote R .take (n)
and R .drop (n)
in place of the two .slice
calls, and would use it that way if I was already using Ramda. But it's fairly clean as is.
Update
An alternate technique might look like this:
const groups = (ns, xs) =>
ns.reduce(({xs, found}, n) => ({
xs: xs.slice(n),
found: [...found, xs.slice(0, n)]
}), {xs, found: []}).found