How to use Javascript to obtain the maximum and minimum values in an array

Source: Internet
Author: User
Comparing the numeric values in an array is a common operation. We will share with you the following four methods to obtain the maximum and minimum values in an array, if you are interested in this, study it and compare the values in the array with common operations. 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.

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.