In JavaScript, you can use the built-in maximum value of Math.max(), but it is still difficult to extract the maximum value from multiple arrays.
Problem description
Suppose you have an array, and this array contains a subarray of numbers, and what we want to do is return its largest maximum number from each subarray in the array.
Basic Solution
function largeOfFour(arr) {var results = []; // Create a result variable to store // Create an outer loop to traverse the outer array for (var n = 0; n < arr.length; n++) {var largeNumber = 0; // Create a second variable to store the largest number // Create another loop to traverse the subarray for (var sb = 0; sb < arr[n].length; sb++) {// Check whether the element of the subarray is greater than the current maximum stored value if (arr[n][sb] > largeNumber) {// If true, assign this value to the variable largeNumberlargestNumber = arr[n][sb];}}// After an internal loop, save the value in each subarray to the array results results[n] = largeNumber;}// Return the array return results;}largestOfFour([[1,34],[456,2,3,44,234],[4567,1,4,5,6],[34,78,23,1]]); //[34, 456, 4567, 78]The above method is a normal solution, traversing the array and its subarrays through two for loops:
Create a results variable to store the maximum value traversed in each subarray
Create an outer loop for traversing outer arrays
Create a second variable, largeNumber, to store the maximum value. This variable value must be placed outside the internal for loop, because it will not be reassigned
Create a second for loop to iterate through each element in the subarray
A if statement is used to determine whether the element of the current subarray is greater than the maximum value stored in the current subarray. If true, store this maximum value to largeNumber .
After the internal loop is finished, the maximum value in each subarray is stored in the initially declared variable results
Finally return results array
After taking out each maximum value in all subarrays, you will get a new array results. At this time, you only need to pass:
Array.prototype.max = function () {return Math.max.apply({},this);}largestOfFour(arr).max();You can get the maximum value.
largeOfFour([[1,34],[456,2,3,44,234],[4567,1,4,5,6],[34,78,23,1]]).max(); // 4567
Intermediate solution
function largeOfFour (arr) {// Use map() method and callback function to combine the maximum values in the subarray together to obtain a new array return arr.map(function (group) {// Use reduce method to return the maximum value in each subarray to the group array return group.reduce(function (prev, current) {// If current is greater than prev, return current, otherwise return prevreturn (current > prev) ? current : prev;});});});} largegestOfFour([[1,34],[456,2,3,44,234],[4567,1,4,5,6],[34,78,23,1]]); // [34, 456, 4567, 78]Use the Array.prototype.map() method to iterate through the array in the outer array. Use the map() method to traverse the array and a callback function will be called. In this callback function, each subarray group is merged using the reduce() method to return the value to a new array. When using the reduce() method, a callback function will also be called. This callback function only does one thing, which is to compare the elements in the subarray. If current is greater than prev, current will be returned, otherwise prev will be returned, and the maximum value in each subarray will be obtained.
As before, the maximum value is finally obtained through Math.max.apply().
The best solution
function largeOfFour (arr) {return arr.map(Function.apply.bind(Math.max, null));}largestOfFour([[1,34],[456,2,3,44,234],[4567,1,4,5,6],[34,78,23,1]]); //[34, 456, 4567, 78]In this scheme, using the Function.bind method to create a special callback function, which is similar to the Math.max method, but it has a Function.prototype.apply function that takes the array as its parameter.
First iterate over each element in the main array, that is, each subarray inside the array
Using the map() method requires a callback function to find the maximum value in each array inside. You need to create a function that allows Math.max to accept input array work. In other words, this is very simple and works very well, such as Math.max([9,43,20,6]); will return the maximum value of 43
The Function.prototype.apply method can accept arrays as parameters, but the function uses the call context, which is a bit complicated. For example, Math.max.apply(null,[9,43,20,6]) will call a Max.max method, but such a method is not easy to find.
Here, a null parameter is passed to the Function.prototype.apply method, telling Math.max that there is no context required.
Because arr.map() requires a callback function, not just an expression, we provide a function in the Function.bind method
Because Function.prototype.apply is a static method, similar to a function object, we can call it Function.prototype.bind bound to Function.prototype.apply. For example: Function.apply.bind
Its context can now be specified via the Function.prototype.apply.bind callback function, such as the Math.max method in this example
Since it is embedded in the Function.prototype.apply method, a context is required as the first parameter, and this context is still a false one.
So we pass null as the second parameter to Function.prototype.apply.bind and bind a context, which is the Math.max method
Since Math.max is independent of any context, it ignores the fake context of the Function.prototype.apply method call
We use Function.prototype.apply.bind(Math.max,null) to make a new function accept arr.map value, such as a subarray in an array
Take the maximum value in a multi-dimensional array
The above uses different methods to extract the maximum values from the subarray from a two-dimensional array and re-form these maximum values into a new array. If you extend it, you also need to use the Array.prototype.max function to obtain the maximum value through Math.max.apply({},this) in the function. However, if it is not a two-dimensional array, the above method will not be able to retrieve the largest value in the array.
To get the maximum value in a multi-dimensional array, you can combine it with the join() and split() methods:
function largeOfFour (arr) {var newArray = arr.join(",").split(",");return Math.max.apply({},newArray);}largestOfFour([12,23]); // =>23largestOfFour([12,23,[1234,324],[345,566]]); // =>1234largestOfFour([12,23,[1234,324,[23121,90890]],[345,566,[345,78,90]]]); // =>1234largestOfFour([12,23,[1234,324,[23121,90890]],[345,566,[345,78,90]]]); // =>90890largestOfFour([12,23,[1234,324,[23121,90890]],[345,566,[345,78,90,[90909090,988]]]]); // =>9090909090You can also use a similar method to remove the minimum value in a multi-dimensional array:
function smallerOfFour (arr) {var newArray = arr.join(",").split(",");return Math.min.apply({},newArray);}smallerOfFour([12,23]); // =>12smallerOfFour([112,23,[1234,324],[345,566]]); // =>23smallerOfFour([212,123,[1234,324,[23121,90890]],[345,566,[345,78,90]]]); // =>23smallerOfFour([212,123,[1234,324,[23121,90890]],[345,566,[345,78,90]]]); // =>78smallerOfFour([102,230,[1234,324,[23121,90890]],[345,566,[345,78,90,[90909090,988]]]]); // =>78Summarize
In the article "JavaScript Learning Notes: Taking Maximum and Minimum Values in Arrays", Math.max.apply({},arr) is introduced to get the largest number in an array. This article introduces how to extract the largest number from a two-dimensional array from different angles, but in many cases, arrays also have multi-dimensional arrays. The article ends up introducing how to extract the largest value from a multi-dimensional array. If you have more solutions, please share them with us in the comments.