复制代码代码如下:
/ ************************************************* ***
* Createby: Joe Zhou
* Description: 数组统计函数
**************************************************** ** /
$ .extend ({
max: fonction (arr) {
return cacl (arr, fonction (item, max) {
if (! (max> item)) {
return item;
}
autre {
retourner max;
}
});
},
min: function (arr) {
return cacl (arr, fonction (item, min) {
if (! (min <item)) {
return item;
}
autre {
retourne min;
}
});
},
sum: function (arr) {
return cacl (arr, fonction (item, sum) {
if (typeof (sum) == 'Undefined') {
return item;
}
autre {
return sum + = item;
}
});
},
avg: fonction (arr) {
if (typeof (arr) == 'Undefined' || arr.length == 0) {
retour 0;
}
retourner this.sum (arr) / arr.length;
}
});
$ .fn.extend ({
max: function () {
return $ .max (this.get ());
},
min: function () {
retourner $ .min (this.get ());
},
sum: function () {
retourner $ .sum (this.get ());
},
avg: fonction () {
retourner $ .avg (this.get ());
}
});
fonction cacl (arr, rappel) {
var ret;
pour (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;
}
autre {
retourner max;
}
});
};
Array.prototype.min = fonction () {
return cacl (this, function (item, min) {
if (! (min <item)) {
return item;
}
autre {
retourne min;
}
});
};
Array.prototype.sum = function () {
return cacl (this, function (item, sum) {
if (typeof (sum) == 'Undefined') {
return item;
}
autre {
return sum + = item;
}
});
};
Array.prototype.avg = function () {
if (this.length == 0) {
retour 0;
}
return this.sum (this) / this.length;
};