How about augmenting the built-in Array object to use Math.max/Math.min instead:
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
let p = [35,2,65,7,8,9,12,121,33,99];
console.log(`Max value is: ${p.max()}` +
`\nMin value is: ${p.min()}`);
Here is a JSFiddle.
Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just apply'ing Math.xxx() to your array directly:
var min = Math.min.apply(null, arr),
max = Math.max.apply(null, arr);
Alternately, assuming your browser supports ECMAScript 6, you can use spread syntax which functions similarly to the apply method:
var min = Math.min( ...arr ),
max = Math.max( ...arr );
Answer from Roatin Marth on Stack OverflowHow about augmenting the built-in Array object to use Math.max/Math.min instead:
Array.prototype.max = function() {
return Math.max.apply(null, this);
};
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
let p = [35,2,65,7,8,9,12,121,33,99];
console.log(`Max value is: ${p.max()}` +
`\nMin value is: ${p.min()}`);
Here is a JSFiddle.
Augmenting the built-ins can cause collisions with other libraries (some see), so you may be more comfortable with just apply'ing Math.xxx() to your array directly:
var min = Math.min.apply(null, arr),
max = Math.max.apply(null, arr);
Alternately, assuming your browser supports ECMAScript 6, you can use spread syntax which functions similarly to the apply method:
var min = Math.min( ...arr ),
max = Math.max( ...arr );
var max_of_array = Math.max.apply(Math, array);
For a full discussion see: http://aaroncrane.co.uk/2008/11/javascript_max_api/
Videos
You'll have to extract the properties from the objects yourself:
var maximum = Math.max.apply(Math, myArr.map(function(o) { return o.x; }));
That uses .map() to iterate through the elements of the array and return the value of the "x" property. That result array is then passed as the arguments to Math.max().
Now that => functions are widely available it'd be a little shorter as so:
var maximum = Math.max.apply(Math, myArr.map(o => o.x));
Still doing pretty much the exact same thing of course.
One more way, I think it is more beautiful. By using spread:
Math.max(...array.map((i) => i.x));
I'm trying to get the maximum value in an array. What's the correct syntax to do this?
Alternative, is there a way to compare different variables and get the name of the variable with the highest value? Like
var memory = 10 var cores = 20 var level = 200 var node = 5 -> return "level"
Can you use Math.max with an array?
No, but...
If you're using Java 8, you can use streams:
Arrays.stream(array).max().getAsInt()
Otherwise you can write a simple utility method to do it for you:
public static int max(int... array) {
if (array.length == 0) {
// ...
}
int max = array[0];
for (int a : array) {
if (a > max)
max = a;
}
return max;
}
// Initializing array of integers
Integer[] num = { 2, 4, 7, 5, 9 };
// using Collections.max() to find minimum element
// using only 1 line.
int max = Collections.max(Arrays.asList(num));