JavaScript 数组中最大最小值
作者:hebedich 时间:2024-04-27 15:23:56
现在获取数组中最大最小值用的越来越多了,于是乎我编了个方法供大家使用。代码如下,若有问题可以与我联系,咱们一起学习一起进步。
我们来看下示例一:
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
我们再来看2个方法
方法一:
//最小值
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() {
... ...
}
}
方法二:
用Math.max和Math.min方法可以迅速得到结果。apply能让一个方法指定调用对象与传入参数,并且传入参数是以数组形式组织的。恰恰现在有一个方法叫Math.max,调用对象为Math,与多个参数
Array.max = function( array ){
return Math.max.apply( Math, array );
};
Array.min = function( array ){
return Math.min.apply( Math, array );
};
但是,John Resig是把它们做成Math对象的静态方法,不能使用大神最爱用的链式调用了。但这方法还能更精简一些,不要忘记,Math对象也是一个对象,我们用对象的字面量来写,又可以省几个比特了。
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
标签:JavaScript,数组,最大最小值
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP中页面限权访问的几种方法
2007-12-13 06:53:00
错误22022 SQLServerAgent当前未运行的解决方法
2024-01-27 21:19:32
浅析JavaScript中的事件机制
2024-05-03 15:58:38
![](https://img.aspxhome.com/file/2023/4/129084_0s.jpg)
PHP xpath提取网页数据内容代码解析
2024-06-05 15:39:35
IE6图片加载的一个BUG解决方法
2023-09-16 03:20:10
PHP判断密码强度的方法详解
2023-06-14 03:00:08
asp事务处理的另外一个方法
2010-05-27 12:18:00
再谈 MySQL 数据库备份恢复和乱码问题
2009-08-19 09:35:00
使用python实现将视频中的音频分离出来
2021-10-20 21:26:07
![](https://img.aspxhome.com/file/2023/8/118048_0s.jpg)
python主线程与子线程的结束顺序实例解析
2023-10-09 12:46:07
python使用自定义钉钉机器人的示例代码
2021-10-21 12:08:02
![](https://img.aspxhome.com/file/2023/7/133807_0s.png)
Python深度学习pytorch神经网络Dropout应用详解解
2023-01-02 11:18:54
![](https://img.aspxhome.com/file/2023/3/115613_0s.png)
django DRF图片路径问题的解决方法
2023-02-05 15:25:46
![](https://img.aspxhome.com/file/2023/7/123617_0s.jpg)
对Python信号处理模块signal详解
2021-08-20 04:06:11
Python算术运算符实例详解
2021-10-05 16:29:22
实例验证MySQL|update字段为相同的值是否会记录binlog
2024-01-21 01:37:40
![](https://img.aspxhome.com/file/2023/4/69774_0s.png)
asp文章中随机插入网站版权文字的实现代码
2011-04-15 11:11:00
Hexo已经看腻了,来手把手教你使用VuePress搭建个人博客
2024-04-26 17:37:08
![](https://img.aspxhome.com/file/2023/8/133458_0s.jpg)
Mysql复合主键和联合主键的区别解析
2024-01-24 04:57:42
python中验证码连通域分割的方法详解
2022-09-30 11:04:00