首页 > 代码库 > JavaScript过滤除连续的数字

JavaScript过滤除连续的数字

if (!Array.prototype.forEach) {    Array.prototype.forEach = function (callback, thisArg) {        var T, k;        if (this == null) {            throw new TypeError(" this is null or not defined");        }        var O = Object(this);        var len = O.length >>> 0; // Hack to convert O.length to a UInt32        if ({}.toString.call(callback) != "[object Function]") {            throw new TypeError(callback + " is not a function");        }        if (thisArg) {            T = thisArg;        }        k = 0;        while (k < len) {            var kValue;            if (k in O) {                kValue = O[k];                callback.call(T, kValue, k, O);            }            k++;        }    };}
//筛选连续的数字        function arrange(source) {            var t;            var ta;            var r = [];            source.forEach(function (v) {                //console.log(t, v);   // 跟踪调试用                if (t === v) {                    ta.push(t);                    t++;                    return;                }                ta = [v];                t = v + 1;                r.push(ta);            });            return r;        }
//测试var arr=[1,5,6,8,11,12];arrange(arr);返回:[[1],[5],[6],[8],[11,12]]

技术交流QQ群:15129679

JavaScript过滤除连续的数字