Now more and more people use the maximum and minimum values in an array, so I have compiled a method for everyone to use. The code is as follows. If you have any questions, please contact me. Let’s learn and make progress together.
Let's take a look at the following example one:
var numReg = /^-?[0-9]+.?[0-9]*$/Array.prototype.min = function() { return this.reduce(function(preValue, curValue,index,array) { if ( numReg.test(preValue) && numReg.test(curValue) ) { return preValue > curValue ? curValue : preValue; } else if ( numReg.test(preValue) ) { return curValue; } else { return 0; } })}Array.prototype.max = function() { return this.reduce(function(preValue, curValue,index,array) { if ( numReg.test(preValue) && numReg.test(curValue) ) { return preValue < curValue ? curValue : preValue; } else if ( numReg.test(preValue) ) { return curValue; } else { return 0; } })}Example 2:
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] alert("aMax:" + getMaximin(a,"max") + "---aMin:" + getMaximin(a,"min") + "---bMax:" + getMaximin(b,"max") + "---bMin:" + getMaximin(b,"min"))//aMax:10---aMin:2---bMax:786---bMin:4 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] alert("aMax:" + getMaximin(a,"max") + "---aMin:" + getMaximin(a,"min") + "---bMax:" + getMaximin(b,"max") + "---bMin:" + getMaximin(b,"min"))//aMax:10---aMin:2---bMax:786---bMin:4Let's look at two more methods
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 and are afraid that the class library also implements a prototype method with the same name, you can make a duplicate name judgment before generating the function:
if (typeof Array.prototype['max'] == 'undefined') { Array.prototype.max = function() { ... ...}}Method 2:
The results can be obtained quickly using the Math.max and Math.min methods. apply can allow a method to specify the call object and the incoming parameters, and the incoming parameters are organized in an array. There is now a method called Math.max, the call object is Math, and it has 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 into Math objects and cannot use the chained calls that the master likes most. But this method can be more streamlined. Don't forget that the Math object is also an object. We can save a few bits when writing it with the literal size of the object.
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