复制代码代码如下:
/********************************************************************************************************** ***
*Createby:Joe Zhou
*描述:数组统计函数
****************************************************** **/
$ .. extend({
max:function(arr){
返回CACL(arr,function(item,max){
如果(!(最大> item)){
返回项目;
}
别的 {
返回最大;
}
});
},,
最低:功能(arr){
返回CACL(arr,function(item,min){
如果(!(min <item)){
返回项目;
}
别的 {
返回最小;
}
});
},,
总和:函数(arr){
返回CACL(arr,function(item,sum){
if(typeof(sum)=='undefined'){
返回项目;
}
别的 {
返回sum += item;
}
});
},,
AVG:功能(arr){
if(typeof(arr)=='undefined'|| arr.length == 0){
返回0;
}
返回this.sum(arr) / arr.length;
}
});
$ ..fn.extend({
max:function(){
返回$ .max(this.get());
},,
min:function(){
返回$ .min(this.get());
},,
sum:function(){
返回$ .sum(this.get());
},,
avg:function(){
返回$ ..avg(this.get());
}
});
功能CACL(ARR,回调){
var ret;
for(var i = 0; i <arr.length; i ++){
ret = callback(arr [i],ret);
}
返回ret;
}
array.prototype.max = function(){
返回CACL(this,函数(item,max){
如果(!(最大> item)){
返回项目;
}
别的 {
返回最大;
}
});
};
array.prototype.min = function(){
返回CACL(此,功能(项目,最小){
如果(!(min <item)){
返回项目;
}
别的 {
返回最小;
}
});
};
array.prototype.sum = function(){
返回CACL(this,函数(item,sum){
if(typeof(sum)=='undefined'){
返回项目;
}
别的 {
返回sum += item;
}
});
};
array.prototype.avg = function(){
if(this.length == 0){
返回0;
}
返回this.sum(this) / this.length;
};