[英]JavaScript: min & max values in very big array?
How can I easily obtain the min and max values from a very big JavaScript Array (eg 250 thousand numbers)? 如何轻松地从很大的JavaScript数组(例如25万个数字)中获取最小值和最大值?
I tried to use this method, but it does not work: JavaScript: min & max Array values? 我尝试使用此方法,但是它不起作用: JavaScript:最小和最大数组值?
I'm debugging in VisualStudio where I don't get any error, while in the browser I get "Maximum call stack size exceeded". 我在Visual Studio中进行调试,没有任何错误,而在浏览器中却显示“超出了最大调用堆栈大小”。
Example code: 示例代码:
var max = Math.max.apply(-Infinity, arr);
var min = Math.min.apply(Infinity, arr);
"arr" is my very big array. “ arr”是我的大阵列。
It is working fine for me 对我来说很好
var min = Math.min.apply(Math, array);
var max = Math.max.apply(Math, array);
For large arrays of mentioned sizes you can not use Math.min()
or Math.max()
safely. 对于上述大小的大型数组,您不能安全地使用
Math.min()
或Math.max()
。 I believe this is how it should be done. 我相信这是应该做的。
var arr = new Array(1000000).fill().map(_=> ~~(Math.random()*10000000)), findMax = a => a.reduce((res,cur) => res < cur ? cur : res ,-Infinity); console.log(findMax(arr));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.