繁体   English   中英

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

[英]Javascript: Using reduce() to find min and max values?

我有一个类的代码,我应该在其中使用 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;
 }

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

 Math.min(...items)

诀窍在于提供一个空数组作为初始值参数

arr.reduce(callback, [initialValue])

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

所以代码看起来像这样:

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;
    }, []);
}

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

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]
    );
}
const values = [1,2,3,4,5];
const [first] = values;
const maxValue = values.reduce((acc, value) => Math.max(acc, value), first);

使用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]));

由于根本不需要reduce调用,您可以从中获得一些乐趣

 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);

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

你可以像这样使用。 可以有任意数量的参数。

function minValue(...args) {
    const min = args.reduce((acc, val) => {
        return acc < val ? acc : val;
    });
    return min;
}

function maxValue(...args) {
    const max= args.reduce((acc, val) => {
        return acc > val ? acc : val;
    });
    return max;
}

使用reduce函数获取数组的最小值和最大值

const ArrayList = [1, 2, 3, 4, 3, 20, 0];
const LargestNum = ArrayList.reduce((prev, curr) => {
      return Math.max(prev, curr)
});
const MinNum = ArrayList.reduce((prev,curr)=>{
      return Math.min(prev,curr)
});
console.log(LargestNum);
console.log(MinNum);

1. 仅使用Math.minMath.max解决方案:

⚠️这不会工作,如果你使用大阵列,即提供Math.min()有许多争论的运行超过了JavaScript引擎的说法长度限制的风险。应用功能与参数太多的后果(认为超过数以万计的参数)因引擎而异(JavaScriptCore 的硬编码参数限制为 65536),因为该限制(甚至任何过大堆栈行为的性质)是未指定的。某些引擎会抛出异常。” 来自 MDN 网络文档

function minMax(items) {
  return [
      Math.min.apply(null, items),
      Math.max.apply(null, items)
  ]
}

...或者如果您更喜欢ES6 的 Spread 语法

const minMax = items => [
  Math.min(...items),
  Math.max(...items)
]

2. 使用Array.prototype.reduce , Math.minMath.max解决方案

function minMax(arr) {
  return arr.reduce(function(acc, cur) {
    return [
      Math.min(cur, acc[0]),
      Math.max(cur, acc[1])
    ]
  }, [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]);
}

...或缩短:

const minMax = items =>
  items.reduce((acc, cur) =>
    [Math.min(cur, acc[0]), Math.max(cur, acc[1])],
    [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]
  )

3. 包括合理验证在内的解决方案

function minMax(items) {
  let newItems = []
  const isArray = Array.isArray(items)
  const onlyHasNumbers = !items.some(i => isNaN(parseFloat(i)))

  // only proceed if items is a non-empty array of numbers
  if (isArray && items.length > 0 && onlyHasNumbers) {
    newItems = items.reduce((acc, cur) => [
        Math.min(cur, acc[0]),
        Math.max(cur, acc[1])
      ], [Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY])
  }

  return newItems
}

Math.min 的文档

Math.max 的文档

Array.prototype.reduce() 的文档

 let arr = [8978, 'lol', -78, 989, NaN, null, undefined, 6, 9, 55, 989]; let minMax = arr.reduce(([min, max], v) => [ Math.min(min, v) || min, Math.max(max, v) || max], [Infinity, -Infinity]); console.log(minMax);

这个怎么运作:

  1. || min || min检查是v数。

  2. [Infinity, -Infinity].reduce初始值

  3. 它使用js 解构赋值

我们可以通过声明一个空数组作为 reduce 函数的累加器值,然后在 reduce 方法的最后一次迭代中执行一组不同的操作来实现这一点。 我们通过将所有四个参数传递给reduce 方法(total、item、index、array)并使用索引与数组长度的比较来在最后一次迭代中做一些不同的事情来做到这一点。

var prices = [32.99, 21.99, 6.99, 4.99, 12.99, 8.98, 5.99];

var highLowPrices = prices.reduce(function(accumulatorArray, price, index, pricesArray){
    if (index === pricesArray.length-1){
        accumulatorArray.push(price);
        var returnArray = [];
        accumulatorArray.sort(function(price1, price2){
            return price1 - price2;
        });
        var lowestPrice = accumulatorArray[0];
        var highestPrice = accumulatorArray[accumulatorArray.length-1];
        returnArray.push(lowestPrice);
        returnArray.push(highestPrice);
        return returnArray;
    } else {
        accumulatorArray.push(price);
        return accumulatorArray;
    }
}, []);

console.log(highLowPrices);

我故意使用了比必要更多的步骤,并使用了语义冗长的变量名称来使逻辑更清晰。

if (index === pricesArray.length-1)表示在通过价格数组的 reduce 方法的最后一次迭代中,发生了一组不同的操作。 到目前为止,我们只是重新创建价格数组,这是微不足道的。 但是在最后一次迭代中,在完全重新创建价格数组之后,我们做了一些不同的事情。 我们创建另一个空数组,我们打算返回的数组。 然后我们对 'accumulatorArray' 变量进行排序 - 这是重新创建的价格数组,从最低到最高对其进行排序。 我们现在取最低价格和最高价格并将它们存储在变量中。 按升序对数组进行排序后,我们知道最低的是索引 0,最高的是索引 array.length - 1。然后我们将这些变量推送到我们之前声明的返回数组中。 而不是返回累加器变量本身,我们返回我们自己特别声明的返回数组。 结果是一个数组,价格最低,然后是最高价格。

这是一个reduce vs Array的例子

const result = Array(-10,1,2,3,4,5,6,7,8,9).reduce((a,b)=>{ return (a<b) ? a : b })

您可能希望使用相同的方法来获取字符串的长度

 const result = Array("ere","reeae","j","Mukono Municipality","Sexy in the City and also").reduce((a,b)=>{ return (a.length<b.length) ? a : b })

我知道这已经得到了回答,但我离开了@Sergey Zhukov 的回答(这似乎不完整)并且能够在 2 行中获得最小值和最大值:

let vals = [ numeric values ]
let min = Math.min.apply(undefined, vals) 
let max = Math.max.apply(undefined, vals)

我看到的价值Array.reduce ,但这样一个超级简单的使用情况,所以只要你明白什么Function.apply做,这将是我转到解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM