Comments (26)
@MillionQW 这是因为发生了隐式类型转换,举个简单例子:
var arr = [6, 4, 1, 8, 2, 11, 23];
console.log(arr + ''); // 6,4,1,8,2,11,23
其实
var max = eval("Math.max(" + arr + ")");
其实就相当于
var max = eval("Math.max(6,4,1,8,2,11,23)");
from blog.
function Max(array) {
return array.reduce((a,b)=>a > b ? a : b)
}
from blog.
@zhqgit var arr = [6, 4, 1, 8, 2, 11, 23];
console.log(...arr ) // 6 4 1 8 2 11 23
from blog.
ES6中也可以用Reflect的方式实现求数组的最大值和最小值
例:var arr = = [1,2,7,9,3,4,5];
Relfect.apply(Math.max,Math,arr) // 9
Reflect.apply(Math.min,Math,arr) //1
from blog.
😊大佬学习了!另外 Reflect 也有一种方式,即:
Reflect.apply(Math.max, Math, [1,2,3])
// -> 3
from blog.
JavaScript 提供了 Math.max 函数返回一组数中的最大值,用法是:
Math.max([value1[,value2, ...]])
这样写容易让人以为传入的是个数组,比如我。。。
from blog.
@shixuev5 感谢回答哈~
@zhqgit 扩展运算符(...)可以将一个数组变为参数序列,引用阮一峰老师的《ECMAScript 6 入门》
from blog.
还以为要从头实现一个underscore的_.max _.min呢
from blog.
@jiangshanmeta 嗯好,四个系列完结后,我会重新修订这些内容,到时候一定补充上这些内容~
from blog.
你好,可以请教一下eval求数组最大值的原理是什么吗?已知给eval()传入字符串,如果字符串是表达式的话就会被执行,但是不明白为什么把Math.max( 和右括号转出字符串中间夹个数组就能求出最大值。
from blog.
@MillionQW 因为数组被转换成字符串了啊。。。
from blog.
@sinkinlife 感谢回答哈~ ( ̄▽ ̄)~*
from blog.
哈哈哈懂了,谢谢两位的回答~
from blog.
@crowphy 哈哈,这个用的是 MDN 的写法,这个方括号是用来表示该参数非必填,可以传也可以不传……如果不用 [] 的话,我不知道对于非必填参数怎么描述了……
from blog.
@Panzhipeng1 好方法,感谢补充哈~
from blog.
@Panzhipeng1 如果传入一组正确的参数给Math.max方法,返回的结果都是"number"类型的,而你这个并没有进行类型转化,两者的结果并不一致。当然这得看实际的需求需不需要进行类型转化,以及“最大”和“最小”是依据什么来判断的。
from blog.
@Tan90Qian 好思路,都没有注意到这一点~ (๑•̀ㅂ•́)و✧
from blog.
用reduce,求最大最小有点小风险,当arr为空数组时,代码运行会报错
所以,运行前最好做个判断
var arr = [ ];
function max(prev, next) {
return Math.max(prev, next);
}
console.log(arr.reduce(max));
from blog.
Math.max.apply(null, arr)
,用对象null调用参数为arr的max方法,这原理为什么能成功?
from blog.
apply()
方法接收两个参数:一个是在其中运行函数的作用域,另一个是参数数组。
简单来说第一个参数就是改变原函数运行时的 this
值。
一些原型上的方法作用的对象是其 this
值。
比如使用 Array.prototype.slice.apply(arguments)
将 arguments
对象转换为数组。
但 max()
是 Math
的静态方法,调用时方法内 this
值对结果没有影响。
from blog.
@mqyqingfeng 楼主好,这也算一种最简单的方法吧,并且有{}或者undefined也能正常运行,function sortArr(arr) {
var max = min = arr[0];
for (var i = 0; i < arr.length; i++) {
arr[i] > max ? max = arr[i] : '';
arr[i] < min ? min = arr[i] : '';
}
return [max, min]
}
console.log(sortArr([11, 3, 5, 3, 66, 22, 66, 33, 565, 2222, 4, 3,{},]))
from blog.
用reduce,求最大最小有点小风险,当arr为空数组时,代码运行会报错
所以,运行前最好做个判断var arr = [ ];
function max(prev, next) {
return Math.max(prev, next);
}
console.log(arr.reduce(max));
你写成这样就不报错了
console.log(arr.reduce(max,[]));
from blog.
Math.max.apply(null, arr)
,用对象null调用参数为arr的max方法,这原理为什么能成功?
和null没什么关系,主要是apply的第二个参数是数组,在没有...运算符前基本都是用apply对数组进行展开的
from blog.
@MillionQW 这是因为发生了隐式类型转换,举个简单例子:
var arr = [6, 4, 1, 8, 2, 11, 23]; console.log(arr + ''); // 6,4,1,8,2,11,23其实
var max = eval("Math.max(" + arr + ")");其实就相当于
var max = eval("Math.max(6,4,1,8,2,11,23)");
var max = Math.max((arr + '')),使用这个为啥就不行了呢,打印出来的是NaN
from blog.
@MillionQW 这是因为发生了隐式类型转换,举个简单例子:
var arr = [6, 4, 1, 8, 2, 11, 23]; console.log(arr + ''); // 6,4,1,8,2,11,23其实
var max = eval("Math.max(" + arr + ")");其实就相当于
var max = eval("Math.max(6,4,1,8,2,11,23)");var max = Math.max((arr + '')),使用这个为啥就不行了呢,打印出来的是NaN
Math.max((arr + '')) 等于Math.max("6,4,1,8,2,11,23"),参数是字符串肯定不行
from blog.
thank you for your provided!
from blog.
Related Issues (20)
- 大佬您好,看完大有收获,但是遇到了个问题,不太会解释它的执行上下文入栈出栈顺序,关于尾递归,顺序是怎样呢? HOT 1
- 冴羽答读者问:如何在工作中打造影响力,带动同事?
- 无
- 冴羽答读者问:如何学习更有计划性、提升更稳更快? HOT 4
- 冴羽答读者问:过程比结果重要吗? HOT 1
- 冴羽答读者问:冴羽,你为什么写起了鸡汤? HOT 3
- 聊聊 npm 的语义化版本(Semver) HOT 4
- How to create Backlinks 😤
- 思考题第2题
- 可以理解为原型是prototype,原型链是通过__proto__ 链接起来的吗
- React 之 createElement 源码解读 HOT 8
- React 之元素与组件的区别 HOT 1
- React 之 Refs 的使用和 forwardRef 的源码解读
- React 之 Context 的变迁与背后实现 HOT 1
- 第一段不报错啊,刚试过了,会打印1
- how can i HOT 1
- Hosting of Blog Issue
- 全局对象
- 为啥每次都要创建一个 Child函数来new 子类?现在不都是 const p1 = new Person(); const p2 = new Person()吗
- 文档内容中文件结构的错位 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.