var numReg = /^-?[0-9]+.?[0-9]*$/
Array.prototype.min = function() {
return this.reduce(function(preValue, curValue,index,array) {
if ( numReg.test(preValue) && numReg.test(curValue) ) {
return preValue > curValue ? curValue : preValue;
} else if ( numReg.test(preValue) ) {
return preValue;
} else if ( numReg.test(curValue) ) {
return curValue;
} else {
return 0;
}
})
}
Array.prototype.max = function() {
return this.reduce(function(preValue, curValue,index,array) {
if ( numReg.test(preValue) && numReg.test(curValue) ) {
return preValue < curValue ? curValue : preValue;
} else if ( numReg.test(preValue) ) {
return preValue;
} else if ( numReg.test(curValue) ) {
return curValue;
} else {
return 0;
}
})
}
function getMaximin (arr,maximin) {
if (maximin == "max") {
return Math.max.apply(Math, arr);
}else if (maximin == "min") {
return Math.min.apply(Math, arr);
}
}
var a = [3,2,4,2,10]
var b = [12,4,45,786,9,78]
alert("aMax:" + getMaximin(a,"max") + "---aMin:" + getMaximin(a,"min") + "---bMax:" + getMaximin(b,"max") + "---bMin:" + getMaximin(b,"min"))//aMax:10---aMin:2---bMax:786---bMin:4
function getMaximin (arr,maximin) {
if (maximin == "max") {
return Math.max.apply(Math, arr);
}else if (maximin == "min") {
return Math.min.apply(Math, arr);
}
}
var a = [3,2,4,2,10]
var b = [12,4,45,786,9,78]
alert("aMax:" + getMaximin(a,"max") + "---aMin:" + getMaximin(a,"min") + "---bMax:" + getMaximin(b,"max") + "---bMin:" + getMaximin(b,"min"))//aMax:10---aMin:2---bMax:786---bMin:4
//最小值
Array.prototype.min = function() {
var min = this[0];
var len = this.length;
for (var i = 1; i < len; i++){
if (this[i] < min){
min = this[i];
}
}
return min;
}
//最大值
Array.prototype.max = function() {
var max = this[0];
var len = this.length;
for (var i = 1; i < len; i++){
if (this[i] > max) {
max = this[i];
}
}
return max;
}
if (typeof Array.prototype['max'] == 'undefined') {
Array.prototype.max = function() {
... ...
}
}
Array.max = function( array ){
return Math.max.apply( Math, array );
};
Array.min = function( array ){
return Math.min.apply( Math, array );
};
Array.prototype.max = function(){
return Math.max.apply({},this)
}
Array.prototype.min = function(){
return Math.min.apply({},this)
}
[1,2,3].max()// => 3
[1,2,3].min()// => 1
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有