简体   繁体   English

如何使用数字数组使用Java中的reduce函数返回最高和最低数字

[英]How do I take an array of numbers and return both the highest and the lowest number using the reduce function in Javascript

How do I take an array of numbers and return both the highest and the lowest number using the reduce function in Javascript? 如何使用数字数组使用Java中的reduce函数返回最高和最低数字?

My attempt so far: 到目前为止,我的尝试:

 function each(collection, callback) {
    if(Array.isArray(collection)) {
       for(var i = 0; i < collection.length; i++) {     
           callback(collection[i]);
    }
  } else {
    for(var prop in collection) {
        callback(collection[prop]);
    }
}
}

function reduce(collection, callback, initial) {
each(collection, function(element) {
    if(initial === undefined) {
        initial = element;
    } else {
        initial = callback(initial, element);
    }
});
return initial;
}

function highAndLow(collection) {
//use reduce
return reduce(collection, function(initial, element){
    if(initial[element] > initial[element]){
        initial = "high: " + element;
    } else {
        initial = "low: " + element
    }
    return initial;
}, {})
}

highAndLow([5,2,1,6,8])

The results are supposed to look like this: highAndLow(5, 2, 1, 6, 8) === { low: 1, high: 8 } 结果应该看起来像这样:highAndLow(5,2,1,6,6,8)=== {低:1,高:8}

Yes, it is possible, for example with this code 是的,有可能,例如使用此代码

 function highAndLow(array) { return array.reduce(function (r, a) { return { low: Math.min(r.low, a), high: Math.max(r.high, a) }; }, { low: array[0], high: array[0] }); } document.write('<pre>' + JSON.stringify(highAndLow([5, 2, 1, 6, 8]), 0, 4) + '</pre>'); document.write('<pre>' + JSON.stringify(highAndLow([]), 0, 4) + '</pre>'); document.write('<pre>' + JSON.stringify(highAndLow([-2, .1, 0, 1, 2]), 0, 4) + '</pre>'); 

function highAndLow() = {
   var args = Array.prototype.call(arguments);
   var low = Infinity;
   var high = -Infinity;
   args.forEach(function (arg) {
      if (arg < low) {
          low = arg;
      }
      if (arg > high) {
          high = arg;
      }
   });
   return {
       low: low,
       high: high
   }
}

or 要么

function highAndLow() {
    return Array.prototype.call(arguments).reduce(function (hash, arg) {
        hash.low = arg < hash.low ? arg : hash.low;
        hash.high = arg > hash.high ? arg : hash.high;
        return hash;
    }, {
       low: Infinity,
       high: -Infinity
    });
}

Here is a solution: 这是一个解决方案:

[-30, 30, 1, 10000, -20, 50, 0].reduce(function(acc, n) {
  if (n < acc.min) {
      return {
        min: n,
        max: acc.max
      }
  }
  if (n > acc.max) {
      return {
        min: acc.min,
        max: n
      }
  }
  return acc
}, {min: Infinity, max: -Infinity})

// {
//   max: 10000,
//   min: -30
// }

We supply an initial value {min: Infinity, max: -Infinity} , with a min which is >= any value we will encounter, and a max which is <= any value we will encounter. 我们提供了一个初始值{min: Infinity, max: -Infinity} ,其中min >= {min: Infinity, max: -Infinity}我们将遇到的任何值,而max <=我们将遇到的任何值。

Then we write our reducing function, which must takes a value like our initial value, and the a value provided by the array. 然后,我们编写我们的reduce函数,该函数必须采用类似于初始值的值,以及数组提供的值。 In our case {min, max} and Number . 在我们的情况下, {min, max}Number Our function simply compares input numbers with the max and min values from the array ensuring that we always return an object with the same properties, max and min . 我们的函数只是将输入数字与数组中的maxmin进行比较,以确保我们始终返回具有相同属性maxmin

These are the basic requirements for any use of reduce . 这些是使用reduce的基本要求。

Incidentally, we can take a very functional and concise approach with es6. 顺便说一下,我们可以在es6中采用非常实用且简洁的方法。

[-30, 30, 1, 10000, -20, 50, 0].reduce(({min, max}, n) => ({
  min: n < min ? n : min,
  max: n > max ? n : max
}), {min: Infinity, max: -Infinity})

With any number of arguments as specified, below can work 使用指定的任意数量的参数,以下可以正常工作

function highAndLow() {
  var inputArr = Array.prototype.slice.call(arguments);
  return inputArr.reduce(function(res, num) {
    if (res === null){
    res = {high:num,low:num};
    }
    if (num > res.high) {
      res.high = num;
    }
    if (num < res.low) {
      res.low = num;
    }
    return res;
  }, null);
}

highAndLow(5, 2, 1, 6, 8) //{high: 8, low: 1}

暂无
暂无

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

相关问题 返回一串带空格的数字中的最高和最低数字 - Return highest and lowest number in a string of numbers with spaces 返回给定字符串的最高和最低编号-Javascript - Return the highest and lowest number of a given string - Javascript 如何在对象数组/ Javascript 中找到最高和最低 - How can I find highest and lowest in an array of objects/ Javascript 使用循环查找数组中的最低/最高数字 - Finding the lowest/highest number in an array using a loop 创建一个随机数数组返回最高数字Javascript - Create an array of random numbers Return highest number Javascript 如何从生成随机数的数组中确定最高和最低数字; JavaScript的 - How to determine the highest and lowest numbers from an array that generates random numbers; JavaScript 减少没有初始值的空数组,如何对具有两个参数的函数从最大值到最小值进行排序 - Reduce of empty array with no initial value, how to sort function with two parameters from highest to lowest value Javascript,从最高到最低对数字数组进行排序,但保留原始索引 - Javascript, Sort an array of numbers from highest to lowest but keep original index 如何使用 for of loop javascript 返回数组中的元素数? - How do I return the number of elements in an array using for of loop javascript? 取得最高的属性值,并将其返回到Javascript数组中 - Take highest property value and return it in an array in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM