1. 问题
js 函数Math.min 和 Math.max 来查找二维数组中的最小值和最大值,在小数组上工作正常但在大数组时报“Maximum call stack size exceeded.”
2.解决方案
const arrayMinMax = (arr) => {
// Math.min 和 Math.max 在大数组时报“Maximum call stack size exceeded.”
return arr.reduce(([min, max], val) => [Math.min(min, val), Math.max(max, val)], [
Number.POSITIVE_INFINITY,
Number.NEGATIVE_INFINITY,
]);
}