富贵资源网 Design By www.hznty.com
复制代码 代码如下:
/****************************************************
*CreateBy:joe zhou
*Description:数组统计函数
****************************************************/
$.extend({
    max: function (arr) {
        return cacl(arr, function (item, max) {
            if (!(max > item)) {
                return item;
            }
            else {
                return max;
            }
        });
    },
    min: function (arr) {
        return cacl(arr, function (item, min) {
            if (!(min < item)) {
                return item;
            }
            else {
                return min;
            }
        });
    },
    sum: function (arr) {
        return cacl(arr, function (item, sum) {
            if (typeof (sum) == 'undefined') {
                return item;
            }
            else {
                return sum += item;
            }
        });
    },
    avg: function (arr) {
        if (typeof (arr) == 'undefined' || arr.length == 0) {
            return 0;
        }
        return this.sum(arr) / arr.length;
    }
});

$.fn.extend({
    max: function () {
        return $.max(this.get());
    },
    min: function () {
        return $.min(this.get());
    },
    sum: function () {
        return $.sum(this.get());
    },
    avg: function () {
        return $.avg(this.get());
    }
});

function cacl(arr, callback) {
    var ret;
    for (var i=0; i<arr.length;i++) {
        ret = callback(arr[i], ret);
    }
    return ret;
}

Array.prototype.max = function () {
    return cacl(this, function (item, max) {
        if (!(max > item)) {
            return item;
        }
        else {
            return max;
        }
    });
};
Array.prototype.min = function () {
    return cacl(this, function (item, min) {
        if (!(min < item)) {
            return item;
        }
        else {
            return min;
        }
    });
};
Array.prototype.sum = function () {
    return cacl(this, function (item, sum) {
        if (typeof (sum) == 'undefined') {
            return item;
        }
        else {
            return sum += item;
        }
    });
};
Array.prototype.avg = function () {
    if (this.length == 0) {
        return 0;
    }
    return this.sum(this) / this.length;
};
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com