var arr = [1,1,1,2,3,4,5,8,10,22,24,25,26,66] var len = arr.length var i = 1, j = 0 var rst = [] for(; i <= len; i++) { if(arr[i]-arr[i-1] !== 1) { i-j===1 ? rst.push(arr[j]) : rst.push(arr.slice(j, i)) j = i } } rst
有一个数组[1,1,1,2,3,4,5,8,10,22,24,25,26,66]
,
请写一个方法把数组变成[1,1,[1,2,3,4,5],8,10,22,[24,25,26],66]
就是里面的顺着的数字归成一个数组,没思路,有没有好的方案?