I hope that, I'm explaining myself right. I need to write a function that gets an array of numbers and returns an array of ranges if there is.
For example:
[1,2,3,5,6,8,9,10]
-----> [[1,2,3],[5,6],[8,9,10]]
I hope that, I'm explaining myself right. I need to write a function that gets an array of numbers and returns an array of ranges if there is.
For example:
[1,2,3,5,6,8,9,10]
-----> [[1,2,3],[5,6],[8,9,10]]
You could check the previous value and insert a new array, if the delta is not one.
var array = [1, 2, 3, 5, 6, 8, 9, 10],
result = array.reduce((r, v, i, { [i - 1]: p }) => {
if (p === v - 1) r[r.length - 1].push(v);
else r.push([v]);
return r;
}, []);
console.log(result);