Javascript to obtain the maximum and minimum values in the array, javascript Array
Comparing the values in an array is a common operation. There are many methods to compare the values. For example, you can use the built-in sort () function. The following describes several methods. The Code is as follows:
Method 1:
// Minimum value Array. prototype. min = function () {var min = this [0]; var len = this. length; for (var I = 1; I <len; I ++) {if (this [I] <min) {min = this [I] ;}} return min;} // maximum value Array. prototype. max = function () {var max = this [0]; var len = this. length; for (var I = 1; I <len; I ++) {if (this [I]> max) {max = this [I] ;}} return max ;}
If you are introducing a class library for development, you are afraid that the class library has also implemented a prototype method of the same name. You can perform the cognominal judgment before generating the function:
if (typeof Array.prototype['max'] == 'undefined') { Array.prototype.max = function() { ... ...}}
Method 2:
The Math. max and Math. min methods can be used to quickly obtain the results. Apply allows a method to specify the call object and input parameters, and the input parameters are organized in arrays. Now, there is a method named Math. max. The called object is Math, which corresponds to multiple parameters.
Array.max = function( array ){ return Math.max.apply( Math, array );};Array.min = function( array ){ return Math.min.apply( Math, array );};
However, John Resig is a static method that makes them Math objects. It cannot be called using a chain that is a favorite of great gods. However, this method can be more streamlined. Don't forget that the Math object is also an object. We can use the literal number of objects to write it, saving several bits.
Array.prototype.max = function(){ return Math.max.apply({},this) } Array.prototype.min = function(){ return Math.min.apply({},this) } [1,2,3].max()// => 3 [1,2,3].min()// => 1
Method 3:
function getMaximin(arr,maximin) { if(maximin=="max") { return Math.max.apply(Math,arr); }else if(maximin=="min") { return Math.min.apply(Math, arr); } } var a=[3,2,4,2,10]; var b=[12,4,45,786,9,78]; console.log(getMaximin(a,"max"));//10console.log(getMaximin(b,"min"));//04
Method 4:
Var a = [1, 2, 3, 5]; alert (Math. max. apply (null, a); // maximum alert (Math. min. apply (null, a); // minimum value
Multi-dimensional arrays can be modified as follows:
Var a = [, 3, [], [, 8]; var ta =. join (","). split (","); // convert to a one-dimensional array alert (Math. max. apply (null, ta); // maximum alert (Math. min. apply (null, ta); // minimum value
The above content is a summary of the methods used to obtain the maximum and minimum values in the array using Javascript shared with you. I hope you will like it.
Articles you may be interested in:
- Js numeric input box (including maximum and minimum limits and rounding)
- The JS random number acquisition function can customize the maximum value of the minimum value.
- How to obtain the minimum and maximum values of an array using JavaScript
- How does JavaScript obtain the maximum and minimum values of arrays?
- JS method for obtaining the maximum value, minimum value, and length of an array