The maximum and minimum values in the JavaScript array, and the javascript array.

Source: Internet
Author: User
Tags javascript array

The maximum and minimum values in the JavaScript array, and the javascript array.

Now, getting the maximum and minimum values in an array is getting more and more, so I have compiled a method for you to use. The Code is as follows. If you have any questions, contact me. Let's learn and make progress together.

Let's take a look at Example 1:

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 preValue;   } else if ( numReg.test(curValue) ) {   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 preValue;   } else if ( numReg.test(curValue) ) {   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:4

Let'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, 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.