I am looking for a nice way to find the maximum ABSOLUTE value of an array.
My array is i.e.
var array = [10,20,40,-30,-20,50,-60];
Then:
Math.max.apply(null,array);
Will result in '50'. But, actually, I want it to return '60'.
The option is to create a second array using Math.abs, but actually I am wondering if the apply function can be combined, so it is one elegant solution.
Given an unsorted array A of N integers, Return maximum value of f(i, j) for all 1 ≤ i, j ≤ N. f(i, j) or absolute difference of two elements of an array A is defined as |A[i] – A[j]| + |i – j|, where |A| denotes the absolute value of A.
abs() The Math. abs() function returns the absolute value of a number.
The Math. max() method returns the number with the highest value.
Math.max.apply(null, array.map(Math.abs));
If you target browsers that don't support Array.prototype.map (IE<=8), use the polyfill or a library like sugar.js.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With