SoFunction
Updated on 2025-04-10

6 ways to implement the maximum and minimum values ​​of arrays in javascript

Given an array [1,8,5,4,3,9,2], write an algorithm to get the maximum value of the array 9 and the minimum value of 1.

1. Extend the min() function and max() function through the prototype attribute

The idea of ​​algorithm 1 is to dynamically update the maximum and minimum values ​​in the custom min() and max() functions by looping the first value and the following values ​​in sequence to find the result.

        // Minimum value         = function () {
            let min = this[0];
            let len = ;
            for (let i = 1; i < len; i++) {
                if (this[i] < min) min = this[i]
            }
            return min
        }
        // Maximum value         = function () {
            let max = this[0];
            let len = ;
            for (let i = 1; i < len; i++) {
                if (this[i] > max) max = this[i]
            }
            return max
        }
        // result        (()); // 1
        (()); // 9

2. With the help of the Math object's min() function and max() function

The main idea of ​​algorithm 2 is to change the function's execution body through the apply() function and pass the array as parameters to the apply() function. In this way, the array can directly call the min() function and max() function of the Math object to get the return value.

         = function(array) {
            return (Math, array)
        }
        // Maximum value         = function (array) {
            return (Math, array)
        }
        // result        ((arr)); // 1
        ((arr)); // 9

3. Optimization of Algorithm 2

In Algorithm 2, we use the min() function and max() function as static functions of Array type, but do not support chain calls. We can use object literals to simplify.

        // Minimum value         = function() {
            return ({}, this)
        }
        // Maximum value         = function () {
            return ({}, this)
        }
        // result        (()); // 1
        (()); // 9

Unlike Algorithm 2, during verification, since the min() function and the max() function are instance methods, it can be called directly through the array.
The first value passed in the apply() function in the above algorithm code is {}, which actually represents the global object of the current execution environment. The second parameter this points to the array to be processed.
Due to the special nature of the apply function, the first parameter is automatically replaced by pointing to the global object when specified as null or undefined, and the original value will be wrapped. So we can also set the first parameter to null and undefined.

4. Use Array type reduce() function

The main idea of ​​algorithm 4 is that the reduce() function does not set the initialValue initial value, and directly uses the first element of the array as the first parameter of the callback function, and compares it with the subsequent value in turn. When you need to find the maximum value, each round of accumulator returns the current value that is large; when you need to find the minimum value, each round of accumulator returns the current value that is small.

        // Minimum value         = function () {
            return ((pre, cur) => {
                return pre < cur ? pre : cur
            })
        }
        // Maximum value         = function () {
            return ((pre, cur) => {
                return pre > cur ? pre : cur
            })
        }
        // result        (()); // 1
        (()); // 9

5. With the Array type sort() function

The main idea of ​​algorithm 5 is to sort the array with the help of the native sort() function of the array. After the sorting is completed, the beginning and end elements are the smallest and largest elements of the array.
If the default sort() function is sorted alphabetically during sorting, the numbers will be processed as strings. For example, the number 18 will be processed as "18", and the number 6 will be processed as "6". When sorting, it is compared according to each bit of the string. Because "1" is smaller than "6", it is smaller than "11" when sorting. This is obviously unreasonable for arrays of numeric types. So we need to do a custom sort.

        let sortArr = ((a, b) => a - b)
        // Minimum value        sortArr[0]
        // Maximum value        sortArr[ - 1]
        // result        (sortArr[0]); // 1
        (sortArr[ - 1]); // 9

6. With the help of ES6 extension operator

        // Minimum value        (...arr)
        // Maximum value        (...arr)
        // result        ((...arr)); // 1
        ((...arr)); // 9

This is the end of this article about 6 methods to implement array maximum and minimum values ​​in JavaScript. For more related javascript array maximum and minimum values, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!