Copy codeThe Code is as follows:
/*************************************** *************
* CreateBy: joe zhou
* Description: array statistics function
**************************************** ************/
$. 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;
};