複製代碼代碼如下:
/************************************************* ************************************************** ******* ***
*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;
};