One possibility is to combine map with splice, taking care not to mutate the original array:
const a = [3, 2, 1, 3];
const b = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];
function slices(sizes, arr){
const clonedArr = [].concat(arr); // clone array to prevent mutation
return sizes.map(size => clonedArr.splice(0,size));
}
console.log(slices(a,b)); // [['a', 'b', 'c'], ['d', 'e'], ['f'], ['g', 'h', 'i']]
Alternatively, avoiding mutation altogether, you could use slice with a slightly convoluted reduce:
const slicesByReduce = (sizes, arr) => sizes
.reduce(({start, acc}, size) => ({
start: start + size,
acc: [...acc, arr.slice(start, start + size)]
}),{
start: 0,
acc: []
}).acc;
console.log(slicesByReduce(a,b)); // [['a', 'b', 'c'], ['d', 'e'], ['f'], ['g', 'h', 'i']]