JavaScript gets the maximum and minimum values in an array summary _javascript tips

Source: Internet
Author: User

Comparing the size of the values in the array is a more common operation, there are several ways to compare the size, such as using the own sort () function, the following methods are described below, the following code:

Method One:

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, fear that the class library also implements a prototype method with the same name, which can be judged before the function is generated:

if (typeof array.prototype[' max '] = = ' undefined ') { 
Array.prototype.max = function () { 
...
}
}

Method Two:

The results can be obtained quickly with Math.max and Math.min methods. Apply enables a method to specify the calling object and incoming arguments, and the incoming arguments are organized in an array format. Just now there is a method called Math.max, which calls the object math, with multiple arguments

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 of making them into the math object, and cannot use the chain Call of the great God's favorite. But this method can be more concise, do not forget that the Math object is also an object, we use the literal volume of objects to write, and can save a few 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 Three:

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"));//10
Console.log (getmaximin (b, "Min"));//04

Method Four:

var a=[1,2,3,5];
Alert (Math.max.apply (null, a));//maximum
alert (Math.min.apply (null, a));//min value

Multidimensional arrays can be modified like this:

var a=[1,2,3,[5,6],[1,4,8]];
var ta=a.join (","). Split (",");//Convert to one-dimensional array
alert (Math.max.apply (Null,ta));//maximum
alert (NULL, TA));//min value

The above is a small compilation for everyone to share the JavaScript to get the maximum and minimum values in the array method rollup, I hope you like.

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.