JavaScript learning notes take the maximum and minimum values in the array, and javascript learning notes

Source: Internet
Author: User

JavaScript learning notes take the maximum and minimum values in the array, and javascript learning notes

Recommended reading:Add, delete, modify, and query the array of JavaScript learning notes

Array Summation Method for JavaScript learning notes

Random sorting of JavaScript learning notes Array

In actual business scenarios, the maximum or minimum values in the array must be retrieved. However, arr. max () and arr. min () are not provided in the array. Can this method be implemented in other ways? So today we will sort out some methods of the maximum and minimum values in the array.

Returns the maximum value of an array.

Let's take a look at the following ideas:

Assign the first element of the array to a variable and use the variable as the maximum value;

Start to traverse the array and compare it with the first element from the second element.

If the current element is greater than the current maximum value, the current element value is assigned to the maximum value.

Move to the next element and continue with the previous step.

When the array element traversal ends, this 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 maxvar max = this [0]; // use the for loop to traverse from the first value of the array (var I = 1; I <this. length; I ++) {// if the current element value is greater than max, assign the current value to maxif (this [I]> max) {max = this [I] ;}// return the maximum value return max ;}

Let's look at an example:

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

In the preceding example, all values in the array are numerical values. What is the effect if the array is not all numerical values? To test first:

var arr = [1,45,23,3,6,2,7,234,56,'2345','a','c'];arr.max(); // 'c'

This is not the expected result. (Click here to find a solution)

After learning for a while ago, we all know that the for loop Performance is worse than forEach (). You can change the above method to the 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

Returns the minimum value of an array.

Similar to the idea of getting the maximum value, we can easily implement the arr. min () method to retrieve the minimum 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 scheme, there can also be other methods, such as using the reduce () method of arrays. Recalling the previous knowledge, the reduce () method can receive a callback function callbackfn, which can take the initial values in the array (preValue) compare with the currently processed array item (curValue) in the array. If the preValue is greater than the curValue value, the returned value is preValue. Otherwise, the returned value is curValue. In this way, the maximum value in the array is extracted:

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 use a similar method to implement the arr. mix () method to retrieve the minimum value in 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 ()

For pure numeric arrays, you can use the built-in functions Math. max () and Math. min () in JavaScript. Use these two built-in functions to find the maximum and maximum values in the array respectively. Before using these two built-in functions to retrieve the maximum and minimum values of an array, you must first learn the Math. max () and Math. min () functions.

Math. max ()

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

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

Math. min ()

Math. min () is the opposite of Math. max (). It returns the minimum value of a set of numbers:

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

If there are no parameters for these functions, the result is-Infinity. If any parameter cannot be converted to a value, the result is NaN. Most importantly, these two functions cannot be directly used for arrays composed of numbers. However, there are some similar methods.

Function. prototype. apply () allows you to call parameters using the provided this and parameter groups and arrays.

// Retrieve the maximum value Array in the Array. max = function (array) {return Math. max. apply (Math, array) ;}; // retrieves the minimum value of Array. min = function (array) {return Math. min. apply (Math, array) ;}; var arr = [7,234, 56]; Array. max (arr); // 234Array. min (arr); // 1

The Math object is also an object and can be written using the literal volume of the object, for example:

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(); // 234arr.min(); // 1

In fact, there are simpler methods. ES2015-based method to implement this function is to use the expansion OPERATOR:

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

This operator expands the values in the array at the position of the function call.

Summary

This article provides several methods to retrieve the maximum and minimum values from the array. These methods only apply to the number array, and how to retrieve only the maximum and minimum values when the array contains other data types (if you know how to implement it, also hope to give you some advice ). Among these methods, using the JavaScript built-in functions Math. max () and Math. min () with Function. prototype. apply () can easily retrieve the maximum and minimum values in the array. Of course, the simplest method is to use the presentation operator in the number ES2015.

The method for taking the maximum and minimum values in the array of JavaScript learning notes is described as follows. If you have a better solution, I hope to share it with us in the following comments.

Articles you may be interested in:
  • How to obtain the minimum and maximum values of an array using JavaScript
  • How does JavaScript obtain the maximum and minimum values of arrays?
  • JS method for obtaining the maximum value, minimum value, and length of an array
  • Javascript to obtain the maximum and minimum values in an array

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.