I have an array that has some values inside, and I wish to return another array that has the value grouped in to their own arrays.
So the result I am trying to achieve is something like this:
var arr = [1,1,2,2,2,3,3,4,4,4,4,5,6]
var groupedArr =[[1,1],[2,2,2],[3,3],[4,4,4,4],[5],[6]]
This proposal works with
Array#reduce
for sorted arrays.With
forEach
and temporary arrayThis may not be the most optimal version but should do. This also works for unsorted arrays.
This is the most straightforward in my mind:
Here you go. By the way, this works with unsorted array as well.
Well this should do. Pretty straight forward.., You get the elements and then remove them.
I think you could use the code below:
Now the groupedArray will contain the Result