function arrange(source) { var t; var ta; var r = []; for(var j=0;j<source.length;j++){ var v=source[j]; if(v!=null){ //console.log(t, v); // 跟踪调试用 if (t === v) { ta.push(t); t++; continue; } ta = [v]; t = v + 1; r.push(ta); } } return r; }
测试:
var arr = [3, 4, 13, 14, 15, 17, 20, 22]; console.log(arrange(arr));
//结果 [[3,4],[13,14,15],[17],[20],[22]]
技术交流QQ群:15129679