整理:Javascript获取数组中的最大值和最小值的方法汇总
方法一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//最小值 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; } |
如果你是引入类库进行开发,害怕类库也实现了同名的原型方法,可以在生成函数之前进行重名判断:
1
2
3
4
5
|
if ( typeof Array.prototype[ 'max' ] == 'undefined' ) { Array.prototype.max = function () { ... ... } } |
方法二:
用Math.max和Math.min方法可以迅速得到结果。apply能让一个方法指定调用对象与传入参数,并且传入参数是以数组形式组织的。恰恰现在有一个方法叫Math.max,调用对象为Math,与多个参数
1
2
3
4
5
6
|
Array.max = function ( array ){ return Math.max.apply( Math, array ); }; Array.min = function ( array ){ return Math.min.apply( Math, array ); }; |
但是,John Resig是把它们做成Math对象的静态方法,不能使用大神最爱用的链式调用了。但这方法还能更精简一些,不要忘记,Math对象也是一个对象,我们用对象的字面量来写,又可以省几个比特了。
1
2
3
4
5
6
7
8
|
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 |
方法三:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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]; console.log(getMaximin(a, "max" )); //10 console.log(getMaximin(b, "min" )); //04 |
方法四:
1
2
3
|
var a=[1,2,3,5]; alert(Math.max.apply( null , a)); //最大值 alert(Math.min.apply( null , a)); //最小值 |
多维数组可以这么修改:
1
2
3
4
|
var a=[1,2,3,[5,6],[1,4,8]]; var ta=a.join( "," ).split( "," ); //转化为一维数组 alert(Math.max.apply( null ,ta)); //最大值 alert(Math.min.apply( null ,ta)); //最小值 |
整理:Javascript获取数组中的最大值和最小值的方法汇总的更多相关文章
- Javascript获取数组中的最大值和最小值的方法汇总
比较数组中数值的大小是比较常见的操作,下面同本文给大家分享四种放哪广发获取数组中最大值和最小值,对此感兴趣的朋友一起学习吧 比较数组中数值的大小是比较常见的操作,比较大小的方法有多种,比如可以使用 ...
- Javascript获取数组中的最大值和最小值方法汇总
方法一 sort()方法 b-a从大到小,a-b从小到大 var max2 = arr.sort(function(a,b){ return b-a; })[0]; console.log(max2) ...
- JAVA 数组实例-求学生成绩的最大成绩,获取数组中的最大值、最小值
实例: import java.util.*; //求学生最大成绩 public class Test{ public static void main(String[] args){ System. ...
- js 获取数组中的最大值和最小值
var arr = [3,12,23,18,25,33,22,30,1] 方案一: 思想 首先对数组进行排序(小 >大),第一项为最小值,最后一项为最大值 var min; var max; a ...
- js获取数组中的最大值/最小值
目录 前言 1. 使用Math的静态方法max/min 1.1 结合ES6的扩展运算符...使用 1.2 结合apply/call方法来使用 1.3 结合reduce来使用 2. 排序获取 2.1 只 ...
- C#获取一个数组中的最大值、最小值、平均值
C#获取一个数组中的最大值.最小值.平均值 1.给出一个数组 ,,,,,-,,,,}; 2.数组Array自带方法 本身是直接可以调用Min(),Max(),Average()方法来求出 最小值.最大 ...
- 求一个number数组中的最大值和最小值的差
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- JavaScript从数组中删除指定值元素的方法
本文实例讲述了JavaScript从数组中删除指定值元素的方法.分享给大家供大家参考.具体分析如下: 下面的代码使用了两种方式删除数组的元素,第一种定义一个单独的函数,第二种为Array对象定义了一个 ...
- 如何使用Math对象快速计算数组中的最大值或最小值
Math 对象下包含 min() 和 max() 方法 用于确定一组数值中的最大值和最小值.这两个方法都可以接收任意多个数值参数. var max = Math.max(1,2,3,4,5,6); c ...
随机推荐
- vs2013 打开vs2010 找不到此项目类型所基于的应用程序 MVC2 升级 MVC5 不能加载Web项目
Upgrading an ASP.NET MVC 2 Project to ASP.NET MVC 3 Tools Update ASP.NET MVC 3 can be installed side ...
- Fabio
Fabio 安装和简单使用 Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用 ...
- [LeetCode] Alien Dictionary 另类字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- Ubuntu下git的安装与使用
Ubuntu下git的安装与使用 Ubuntu下git的安装与使用与Windows下的大致相同,只不过个人感觉在Ubuntu下使用git更方便. 首先,确认你的系统是否已安装git,可以通过git指令 ...
- mysqld初探
一.简介 deamon是守护神的意思,表示守护进程.mysqld就是mysql的服务器端,就是基于socket的一个服务器端程序,它始终监听3306端口(默认端口).mysql是客户端程序. 安装my ...
- c#线程带参数
c#线程带参数 ThreadStart threadStart = delegate { LoadPicture(ds.Tables[0]); }; Thread thread = new Threa ...
- CentOS升级openssl
才设置了http2,结果蓝狗说我网站不安全,检测一下发现openssl有漏洞,于是准备升级一下openssl 检测网站: www.ssllabs.com/ssltest/analyze.html # ...
- Python从破门而入到夺门而出
MD版网盘备份: 链接: https://pan.baidu.com/s/1kVJNRSz 密码: agxt 基于<简明Python教程> 一.Python概览 1.使用PyCharm是非 ...
- 搜索框(Thinkphp5.0)
1.普通关键词搜索框 模板部分代码: <form name='searchform' action='/index.php/module/controller/search' method='g ...
- Linux虚拟机中配置JDK环境变量
前提准备: 1,安装好Linux系统 2,下载好可以将文件传输到Linux系统工具例如:WinSCP 3,在windows中下载Linux版JDK: http://download.oracle.co ...