I've a JavaScript array and sum as input

```
array = [4,8,2,4,2,2,8,12,4,2, 2]
sum = 12 // all the time sum will be 12

```

I want 2d array, the numbers in batches should be sum equals or less than 12

The output array should look like

```
[
  [4,8],
  [2,4,2,2,2],
  [8, 4],
  [12],
  [2]
]

4 + 8 = 12
2 + 4 + 2 + 2 + 2 = 12
...
2 is left at the end

```

Other examples

1) array = [6,5,3,3,3,2,2,2,2]
sum = 12

output: [ [6,3,3], [5,3,2,2], [2,2] ]

One the number is allotted to subset, it should not used to other subset

remaining numbers can be added to the last but sum should be less than 12, else add one more array and add remaining ones

> 
> The input array can have any integer from 1 - 12
> 
> 
> 

How can I get the output I want?