how to create new array with cumulative numbers last index to next cumulative first index.
I have an array-like
const numbers = [ '1', '2', '3', '16', '17', '18', '19', '31', '32', '42', '43', '53', '54', '58', '59', '69', '70', '81', '82', '103', '104', '115', '116']
The expected output will be like
const newArray = [['3', '16'], ['19','31'], ['32','42'], ['43', '53'] ... ]
There is always at least two cumulative number. I tried
const numbers = ['1', '2', '3', '16', '17', '18', '19', '31', '32', '42', '43', '53', '54', '58', '59', '69', '70', '81', '82', '103', '104', '115', '116']
const reGroup = []
const findGroup = numbers.reduce((prev, current) => {
data = []
if (prev == (current - 1)) {
data.push(prev, current)
}
reGroup.push(data)
})
console.log(reGroup)