How to batch group of array elements in reference to another array of group size ? I tried below code.
Code:
var group_size = [1, 3, 5];
var elements = ['a','b','c','d','e','f','g','h','i','j','k','l'];
var output = [];
for (var i=0; i < group_size.length; i++) {
output.push(elements.slice(i, group_size[i]))
}
console.log(output);
Output:
[["a"], ["b", "c"], ["c", "d", "e"]]
But expected output:
[['a'], ['b','c','d'],['e','f','g','h','i'],['j','k','l']]
If there are moe elements, then those elements to be grouped by max group_size element.
Example :
Input = ['a','b','c']
Output = [['a'], ['b','c']]
Input = ['a','b','c','d','e']
Output = [['a'], ['b','c','d'], ['e']]
Input = ['a','b','c','d','e','f','g','h','i','j','k','l']
Output = [['a'], ['b','c','d'],['e','f','g','h','i'],['j','k','l']]
Input = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p']
Output = [['a'], ['b','c','d'],['e','f','g','h','i'],['j','k','l','m','n'], ['o','p']]
I tried above code but that's limiting. How can I do it in ecma5 (js) ?
What do you need for the solution: