Rië*_*iël 6 javascript arrays math max absolute
我正在寻找一种很好的方法来找到数组的最大ABSOLUTE值.
我的阵列是ie
var array = [10,20,40,-30,-20,50,-60];
Run Code Online (Sandbox Code Playgroud)
然后:
Math.max.apply(null,array);
Run Code Online (Sandbox Code Playgroud)
将导致'50'.但实际上,我希望它返回'60'.
选项是使用Math.abs创建第二个数组,但实际上我想知道是否可以组合apply函数,所以它是一个优雅的解决方案.
Tom*_*lak 17
Math.max.apply(null, array.map(Math.abs));
Run Code Online (Sandbox Code Playgroud)
如果定位的浏览器不支持Array.prototype.map(IE <= 8),使用的填充工具或类似的的文库sugar.js.