JavaScript Learning notes _javascript tips for maximum and minimum values in an array

Source: Internet
Author: User
Tags numeric value

Recommended reading: Add, delete, change and check the array of JavaScript learning notes

An array summation method of JavaScript learning notes

A random sort of array of JavaScript learning notes

The maximum or minimum value in an array is sometimes taken out of the actual business. However, the Arr.max () and Arr.min () methods are not provided in the array. So is it possible to implement a method like this in some other way? So today we're going to sort out some of the methods for removing the maximum and minimum values from the array.

Take the maximum value in an array

You can put your thoughts first:

Assigns the first element in the array to a variable, and takes the variable as the maximum value;

Begins traversing the array, starting with the second element and then comparing the first element

Assigns the current element value to the maximum if the current element is greater than the current maximum value

Move to the next element and continue to the previous step

When the array element traversal ends, the variable stores the maximum value

The code is as follows:

Array.prototype.max = function () {
//Assign the value of the first element of the array to Max
var max = this[0];
Use the For loop to start with the first value of the array as the traversal
for (var i = 1; i < this.length i++) {
//if the current value of the element is greater than Max, assign the current value to Max
if (This[i] & Gt Max) {
max = this[i];
}
Returns the maximum value return
max;
}

To see an example:

var arr = [1,45,23,3,6,2,7,234,56];
Arr.max (); 234

The example above, which is all numeric in the array, what would be the effect if the array is not all numeric? To test first:

var arr = [1,45,23,3,6,2,7,234,56, ' 2345 ', ' A ', ' C '];
Arr.max (); C

This is not the result we want. (Kneel here for solution)

By learning from the previous period, you know that for loop performance is worse than foreach (), you can change the above method to a foreach () method:

Array.prototype.max = function () {
var max = this[0];
This.foreach (function (Ele,index,arr) {
if (Ele > Max) {
max = ele;
}
})
return max;
}
var arr = [1,45,23,3,6,2,7,234,56];
Arr.max (); 234

Take the smallest value in an array

Like the idea of taking the maximum, we can easily implement the Arr.min () method to remove the smallest value in the array:

Array.prototype.min = function () {
var min = this[0];
This.foreach (function (Ele, Index,arr) {
if (Ele < min) {
min = ele;
}
})
return min;
}
var arr = [1,45,23,3,6,2,7,234,56];
Arr.min (); 1

Other methods

In addition to the above scenario, there are other ways to do this, such as using the reduce () method of arrays. Recalling the previous learned knowledge, the reduce () method can receive a callback function CALLBACKFN, in which the initial value (Prevalue) in the array is compared to the array item (CURVALUE) that is currently being processed in the array. If Prevalue is greater than Curvalue value returns Prevalue, return Curvalue value, and so on, and then remove the maximum value from the array:

Array.prototype.max = function () {return
this.reduce (function (Prevalue, curvalue,index,array) {
return Prevalue > Curvalue? Prevalue:curvalue
})
}
var arr = [1,45,23,3,6,2,7,234,56];
Arr.max (); 234

Similarly, you can implement the Arr.mix () method by using a similar method to remove the minimum value from the array:

Array.prototype.min = function () {return
this.reduce (function (Prevalue, curvalue,index,array) {
return Prevalue > Curvalue? Curvalue:prevalue
})
}
var arr = [1,45,23,3,6,2,7,234,56];
Arr.min (); 1

Built-in functions Math.max () and Math.min () methods

For a pure numeric array, you can use the built-in functions in JavaScript Math.max () and the Math.min () method. Using these two built-in functions, you can find the largest and highest values in the array, respectively. Before using both of these built-in functions to take out the maximum and minimum values of the array, learn the Math.max () and Math.min () two functions.

Math.max ()

The Math.max () function returns the maximum value in a set of numbers.

Math.max (1,32,45,31,3442,4); 3442
Math.max;//
Math.max ( -10, -20);// -10
Math.max (-10, 20);//20

Math.min ()

The Math.min () function is just the opposite of the Math.max () function, which returns the smallest value in a set of numbers:

Math.min (10,20);
math.min ( -10,-20);//-20
math.min ( -10,20);//-10 math.min
(1,32,45,31,3442,4);//1

These functions are-infinity if there are no arguments, and the result is NaN if either argument cannot be converted to a numeric value. The main thing is that these two functions cannot be used directly for arrays of numbers. However, there are some similar methods.

Function.prototype.apply () allows you to invoke parameters using the supplied this and the array of parameters.

Remove the maximum value
Array.max = function (array) {return
Math.max.apply (Math, array) in the array;
Remove the minimum value
array.min = function (array) {return
Math.min.apply (Math, array) in the array;
var arr = [1,45,23,3,6,2,7,234,56];
Array.max (arr); 234
array.min (arr);//1

The Math object is also an object that can be written using the literal volume of an object, such as:

Array.prototype.max = function () {return
Math.max.apply ({},this);
}
Array.prototype.min = function () {return
Math.min.apply ({},this);
}
var arr = [1,45,23,3,6,2,7,234,56];
Arr.max (); 234
arr.min ();//1

In fact, there is a simpler way. The ES2015 method is used to implement this function by using the expand operator:

var numbers = [1, 2, 3, 4];
Math.max (... numbers)//4
math.min (... numbers)//1

This operator causes the values in the array to be expanded at the position of the function call.

Summarize

This article collates several ways to remove the maximum and minimum values from an array. All of these methods are only for numeric arrays, and for arrays that contain other data types, how to remove only the largest and smallest values (if you know how to do that). Among these methods, the built-in functions of JavaScript, Math.max () and Math.min (), together with Function.prototype.apply (), can easily take out the maximum and minimum values in the array. Of course, the easiest way to do this is to use the display operator in several ES2015.

About JavaScript Learning notes in the array of the maximum and minimum value of the method to introduce you so much, if you have a better plan, hope to be in the comments below to share with us.

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.