The simplest way to obtain the maximum (minimum) value in an array is to traverse the array and find its maximum (minimum) value through comparison. But in fact, some shortcuts have been provided in the native method of javascript to achieve this function. You may need to know how to obtain the maximum (minimum) value of an array) the simplest method of value is to traverse the array and find its maximum (minimum) value through comparison. However, some shortcuts have been provided in the native javascript method to implement this function.
1 Array. prototype. sort
The Code is as follows:
Var a = [7, 3, 4, 6, 10];
A. sort (function (a, B ){
Return (a-B );})
Note that the comparison function in sort must be passed in. If this function is not passed in, the result of a. sort () is [, 3, 7];
2 Math. max, Math. min
The Code is as follows:
Var a = [7, 3, 4, 6, 10];
Var max = Math. max. apply (Math, );
Var min = Math. min. apply (Math, );