使用JavaScript·求数组的最大值和最小值
arr.max()
和arr.min()
这样的方法。那么是不是可以通过别的方式实现类似这样的方法呢?那么今天我们就来整理取出数组中最大值和最小值的一些方法。var arr=[,,,,,];
Math.max(...arr); //
Math.min(...arr); //
Array.prototype.max=function(){
let max=this[];
this.forEach(function(item,index){
if(item>max){
max=item;
}
});
return max;
}
var arr = [,,,,,,,,,,,];
console.time("费时");
console.log(arr.max()); //
console.timeEnd("费时"); // 费时:0.376ms
// ================================================ //
Array.prototype.max=function(){
let max=this[];
this.map(function(item,index){
if(item>max){
max=item;
}
});
return max;
}
var arr = [,,,,,,,,,,,];
console.time("费时");
console.log(arr.max()); //
console.timeEnd("费时"); // 费时: 0.402ms // ================================================= //
Array.prototype.max=function(){
let max=this[];
for(var i=;i<this.length;i++){
if(this[i]>max){
max=this[i];
}
}
return max;
}
var arr = [,,,,,,,,,,,];
console.time("费时");
console.log(arr.max()); //
console.timeEnd("费时"); // 费时: 0.522ms
var arr = [1,45,23,3,6,2,7,234,56,'2345',5,'a',1000];
arr.max(); // "a"
['a',1000,'c'].max(); // "c"
[1000,'a'].max(); //
"A".charCodeAt(); //
"a".charCodeAt(); //
Array.prototype.max=function(){ return arr.reduce(function(prev,next){
return prev>next?prev:next;
});
}
var arr = [1,45,23,3,6,2,7,234,56,222,34444,9];
console.time("费时");
console.log(arr.max()); //
console.timeEnd("费时"); // 费时: 0.389ms
求数组最小值的就不再多说了,同理易得~
总结
上面求数组的最值最简单的还是法一,剩下的都是遍历数组,对于数组的遍历也要选择好方法,不同的遍历方法性能不同,其中 Array.prototype.map虽然使用起来优雅,但是实际性能并不会比forEach好,具体的还是示情况而定吧
使用JavaScript·求数组的最大值和最小值的更多相关文章
- JavaScript 获取数组的最大值和最小值
js获取数组最大值和最小值 使用apply方法: var a = [1,2,3,5]; console.log(Math.max.apply(null, a));//最大值 console.log(M ...
- JavaScript 获取数组中最大值、最小值
笨方法 Array.prototype.max = function() { var max = this[0]; var len = this.length; for (var i = 1; i & ...
- JS创建一个数组1.求和 2.求平均值 3.最大值 4.最小值 5.数组逆序 6.数组去重 0.退出
rs = require("readline-sync"); let arr = []; console.log("请输入数组的长度:"); let arr_l ...
- JavaScript学习:取数组中最大值和最小值
在实际业务中有的时候要取出数组中的最大值或最小值.但在数组中并没有提供arr.max()和arr.min()这样的方法.那么是不是可以通过别的方式实现类似这样的方法呢?那么今天我们就来整理取出数组中最 ...
- Javascript获取数组中最大和最小值
取出数组中最大值或最小值是开发中常见的需求,今天继续讲解如何获取javascript数组中最大和最小值. 1.排序法 首先我们给数组进行排序,可以按照从小到大的顺序来排,排序之后的数组中第一个和最后一 ...
- js求数组的最大值--奇技淫巧和笨方法
写这篇文章的原因 我目前做的项目很少用到算法,于是这方面的东西自然就有点儿生疏.最近的一次编码中遇到了从数组中获取最大值的需求,当时我不自觉的想到了js的sort()函数,现在想来真是有些“罪过”,当 ...
- c# 求数组的最大值
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- YTU 2642: 填空题:类模板---求数组的最大值
2642: 填空题:类模板---求数组的最大值 时间限制: 1 Sec 内存限制: 128 MB 提交: 646 解决: 446 题目描述 类模板---求数组的最大值 找出一个数组中的元 ...
- 转载——JavaScript学习笔记:取数组中最大值和最小值
转载自:http://www.w3cplus.com/javascript/calculate-the-max-min-value-from-an-array.html. 取数组中最大值 可以先把思路 ...
随机推荐
- 牛客多校第六场 C Generation I 组合数学 阶乘逆元模板
链接:https://www.nowcoder.com/acm/contest/144/C来源:牛客网 Oak is given N empty and non-repeatable sets whi ...
- HDU - 3966 树链刨分
题目传送门 操作就是询问某个点的值, 然后就是对一条路径上的值全部修改. 最基本的树刨题目了. 树刨的思想: 1. 对于每个点找到他的重儿子. void dfs1(int o, int u){ sz[ ...
- Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.catalina.connector.CoyoteWriter and no properties discovered to create BeanSerializer
一.什么是序列化In computer science, in the context of data storage, serialization is the process of transla ...
- 前端-HTML-web服务本质-HTTP协议-请求-标签-01(待完善)
目录 前端 什么是前端 什么是后端 学习流程 前端三剑客的形容 web服务的本质 测试--浏览器作为客户端向服务器发起请求 浏览器输入网址回车发生了几件事 ***** HTTP协议(超文本传输协议) ...
- 并发之初章Java内存模型
>>>>>>博客地址<<<<<< >>>>>>首发博客<<<<< ...
- ExpandableListView 可折叠的下拉listview
ExpandableListView用法如下 1.定义布局文件main.xml文件 <?xml version="1.0" encoding="utf-8" ...
- Spring Security Oauth2 自定义 OAuth2 Exception
付出就要得到回报,这种想法是错的. 前言 在使用Spring Security Oauth2登录和鉴权失败时,默认返回的异常信息如下 { "error": "unauth ...
- Hibernate,一对多,多对一
Hibernate环境的配置 hibernate.cfg.xml的配置 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibern ...
- Laravel 5.4 快速开发简书:
Laravel 5.4 快速开发简书第1章 课程介绍 介绍课程的大体脉络和课程安排 第2章 Laravel 5.4介绍 本节课会带领大家介绍laravel的各个版本历史以及讨论php框架的未来发展趋势 ...
- Elastic Stack 笔记(六)Elasticsearch5.6 搜索详解
博客地址:http://www.moonxy.com 一.前言 Elasticsearch 主要包含索引过程和搜索过程. 索引过程:一条文档被索引到 Elasticsearch 之后,默认情况下 ES ...