Javascript:使用reduce()查找最小值和最大值?

时间:2022-09-28 10:58:51

I have this code for a class where I'm supposed to use the reduce() method to find the min and max values in an array. However, we are required to use only a single call to reduce. The return array should be of size 2, but I know that the reduce() method always returns an array of size 1. I'm able to obtain the minimum value using the code below, however I don't know how to obtain the max value in that same call. I assume that once I do obtain the max value that I just push it to the array after the reduce() method finishes.

我有一个类的代码,我应该使用reduce()方法来查找数组中的最小值和最大值。但是,我们只需要使用一次调用来减少。返回数组的大小应为2,但我知道reduce()方法总是返回一个大小为1的数组。我可以使用下面的代码获取最小值,但是我不知道如何获取同一个电话中的最大值。我假设一旦我获得了最大值,我只需在reduce()方法完成后将其推送到数组。

/**
 * Takes an array of numbers and returns an array of size 2,
 * where the first element is the smallest element in items,
 * and the second element is the largest element in items.
 *
 * Must do this by using a single call to reduce.
 *
 * For example, minMax([4, 1, 2, 7, 6]) returns [1, 7]
 */
function minMax(items) {
     var minMaxArray = items.reduce(
        (accumulator, currentValue) => {
             return (accumulator < currentValue ? accumulator : currentValue);
        }
    );

     return minMaxArray;
 }

5 个解决方案

#1


6  

The trick consist in provide an empty Array as initialValue Parameter

技巧包括提供一个空数组作为initialValue参数

arr.reduce(callback, [initialValue])

initialValue [Optional] Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used.

initialValue [可选]用作第一次调用回调的第一个参数的值。如果未提供初始值,则将使用数组中的第一个元素。

So the code would look like this:

所以代码看起来像这样:

function minMax(items) {
    return items.reduce((acc, val) => {
        acc[0] = ( acc[0] === undefined || val < acc[0] ) ? val : acc[0]
        acc[1] = ( acc[1] === undefined || val > acc[1] ) ? val : acc[1]
        return acc;
    }, []);
}

#2


7  

In ES6 you can use spread operator. One string solution:

在ES6中,您可以使用扩展运算符。一串解决方案:

 Math.min(...items)

#3


3  

The solution using Math.min() and Math.max() functions:

使用Math.min()和Math.max()函数的解决方案:

function minMax(items) {
    var minMaxArray = items.reduce(function (r, n) {
            r[0] = (!r[0])? n : Math.min(r[0], n);
            r[1] = (!r[1])? n : Math.max(r[1], n);
            return r;
        }, []);

    return minMaxArray;
}

console.log(minMax([4, 1, 2, 7, 6]));

#4


3  

You can use array as return value:

您可以使用数组作为返回值:

function minMax(items) {
    return items.reduce(
        (accumulator, currentValue) => {
            return [
                Math.min(currentValue, accumulator[0]), 
                Math.max(currentValue, accumulator[1])
            ];
        }, [Number.MAX_VALUE, Number.MIN_VALUE]
    );
}

#5


1  

As the reduce call isn't really needed at all, you could have some fun with it

由于根本不需要减少呼叫,您可以享受它的乐趣

let items = [62, 3, 7, 9, 33, 6, 322, 67, 853];

let arr = items.reduce((w,o,r,k,s=Math)=>[s.min.apply(0, k),s.max.apply(0, k)],[]);

console.log(arr);

All you'd really need is let minMaxArray = [Math.min.apply(0,items), Math.max.apply(0,items)]

所有你真正需要的是让minMaxArray = [Math.min.apply(0,items),Math.max.apply(0,items)]

#1


6  

The trick consist in provide an empty Array as initialValue Parameter

技巧包括提供一个空数组作为initialValue参数

arr.reduce(callback, [initialValue])

initialValue [Optional] Value to use as the first argument to the first call of the callback. If no initial value is supplied, the first element in the array will be used.

initialValue [可选]用作第一次调用回调的第一个参数的值。如果未提供初始值,则将使用数组中的第一个元素。

So the code would look like this:

所以代码看起来像这样:

function minMax(items) {
    return items.reduce((acc, val) => {
        acc[0] = ( acc[0] === undefined || val < acc[0] ) ? val : acc[0]
        acc[1] = ( acc[1] === undefined || val > acc[1] ) ? val : acc[1]
        return acc;
    }, []);
}

#2


7  

In ES6 you can use spread operator. One string solution:

在ES6中,您可以使用扩展运算符。一串解决方案:

 Math.min(...items)

#3


3  

The solution using Math.min() and Math.max() functions:

使用Math.min()和Math.max()函数的解决方案:

function minMax(items) {
    var minMaxArray = items.reduce(function (r, n) {
            r[0] = (!r[0])? n : Math.min(r[0], n);
            r[1] = (!r[1])? n : Math.max(r[1], n);
            return r;
        }, []);

    return minMaxArray;
}

console.log(minMax([4, 1, 2, 7, 6]));

#4


3  

You can use array as return value:

您可以使用数组作为返回值:

function minMax(items) {
    return items.reduce(
        (accumulator, currentValue) => {
            return [
                Math.min(currentValue, accumulator[0]), 
                Math.max(currentValue, accumulator[1])
            ];
        }, [Number.MAX_VALUE, Number.MIN_VALUE]
    );
}

#5


1  

As the reduce call isn't really needed at all, you could have some fun with it

由于根本不需要减少呼叫,您可以享受它的乐趣

let items = [62, 3, 7, 9, 33, 6, 322, 67, 853];

let arr = items.reduce((w,o,r,k,s=Math)=>[s.min.apply(0, k),s.max.apply(0, k)],[]);

console.log(arr);

All you'd really need is let minMaxArray = [Math.min.apply(0,items), Math.max.apply(0,items)]

所有你真正需要的是让minMaxArray = [Math.min.apply(0,items),Math.max.apply(0,items)]