Giter VIP home page Giter VIP logo

Comments (26)

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024 16

@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.

 avatar commented on May 2, 2024 5
function Max(array) {
    return array.reduce((a,b)=>a > b ? a : b)
}

from blog.

shixuev5 avatar shixuev5 commented on May 2, 2024 3

@zhqgit var arr = [6, 4, 1, 8, 2, 11, 23];
console.log(...arr ) // 6 4 1 8 2 11 23

from blog.

hubvue avatar hubvue commented on May 2, 2024 3

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.

gongph avatar gongph commented on May 2, 2024 2

😊大佬学习了!另外 Reflect 也有一种方式,即:

Reflect.apply(Math.max, Math, [1,2,3])
// -> 3

from blog.

crowphy avatar crowphy commented on May 2, 2024 1

JavaScript 提供了 Math.max 函数返回一组数中的最大值,用法是:
Math.max([value1[,value2, ...]])

这样写容易让人以为传入的是个数组,比如我。。。

from blog.

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024

@shixuev5 感谢回答哈~
@zhqgit 扩展运算符(...)可以将一个数组变为参数序列,引用阮一峰老师的《ECMAScript 6 入门》

default

from blog.

jiangshanmeta avatar jiangshanmeta commented on May 2, 2024

还以为要从头实现一个underscore的_.max _.min呢

from blog.

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024

@jiangshanmeta 嗯好,四个系列完结后,我会重新修订这些内容,到时候一定补充上这些内容~

from blog.

MillionQW avatar MillionQW commented on May 2, 2024

你好,可以请教一下eval求数组最大值的原理是什么吗?已知给eval()传入字符串,如果字符串是表达式的话就会被执行,但是不明白为什么把Math.max( 和右括号转出字符串中间夹个数组就能求出最大值。

from blog.

roadwild avatar roadwild commented on May 2, 2024

@MillionQW 因为数组被转换成字符串了啊。。。

from blog.

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024

@sinkinlife 感谢回答哈~ ( ̄▽ ̄)~*

from blog.

MillionQW avatar MillionQW commented on May 2, 2024

哈哈哈懂了,谢谢两位的回答~

from blog.

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024

@crowphy 哈哈,这个用的是 MDN 的写法,这个方括号是用来表示该参数非必填,可以传也可以不传……如果不用 [] 的话,我不知道对于非必填参数怎么描述了……

from blog.

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024

@Panzhipeng1 好方法,感谢补充哈~

from blog.

Tan90Qian avatar Tan90Qian commented on May 2, 2024

@Panzhipeng1 如果传入一组正确的参数给Math.max方法,返回的结果都是"number"类型的,而你这个并没有进行类型转化,两者的结果并不一致。当然这得看实际的需求需不需要进行类型转化,以及“最大”和“最小”是依据什么来判断的。

from blog.

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024

@Tan90Qian 好思路,都没有注意到这一点~ (๑•̀ㅂ•́)و✧

from blog.

aierong avatar aierong commented on May 2, 2024

用reduce,求最大最小有点小风险,当arr为空数组时,代码运行会报错
所以,运行前最好做个判断

var arr = [ ];

function max(prev, next) {
return Math.max(prev, next);
}
console.log(arr.reduce(max));

from blog.

MoYummy avatar MoYummy commented on May 2, 2024

Math.max.apply(null, arr),用对象null调用参数为arr的max方法,这原理为什么能成功?

from blog.

inottn avatar inottn commented on May 2, 2024

@MoYummy

apply() 方法接收两个参数:一个是在其中运行函数的作用域,另一个是参数数组。
简单来说第一个参数就是改变原函数运行时的 this 值。
一些原型上的方法作用的对象是其 this 值。
比如使用 Array.prototype.slice.apply(arguments)arguments 对象转换为数组。

max()Math 的静态方法,调用时方法内 this 值对结果没有影响。

from blog.

world8023 avatar world8023 commented on May 2, 2024

@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.

angelayun avatar angelayun commented on May 2, 2024

用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.

TyrionJYQ avatar TyrionJYQ commented on May 2, 2024

Math.max.apply(null, arr),用对象null调用参数为arr的max方法,这原理为什么能成功?
和null没什么关系,主要是apply的第二个参数是数组,在没有...运算符前基本都是用apply对数组进行展开的

from blog.

Quantum-Boy avatar Quantum-Boy commented on May 2, 2024

@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.

dzsqe avatar dzsqe commented on May 2, 2024

@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.

caowanjing-code avatar caowanjing-code commented on May 2, 2024

thank you for your provided!

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.