I am trying to obtain a series of subsets from an array in javascript using underscore. This is what I would like to achieve:
Original array: [1,1,1,2,2,2,0,0,0,1,1,1]
Expected result: [1,1,1] [2,2,2] [0,0,0] [1,1,1]
When I use the filter method from underscore, I get three arrays: [1,1,1,1,1,1] [2,2,2] [0,0,0]; I would like that the last array of 1's would not mix.
What I've tried:
_.filter(array, function(e){
return e === 1;
})
The criteria to split the original array is that the consecutive equal numbers must form a new array, but if the number appears later in the original array, that consecutive equal number must form a new array.
Is there a way to do this using underscore or should it be done with loops?
Thanks