[英]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
. 我们的函数只是将输入数字与数组中的
max
和min
进行比较,以确保我们始终返回具有相同属性max
和min
。
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.