[英]discrepancy in array.reduce javascript
I spent hours trying to debug a particular code, at the very end i noticed something i don't understand with arr reduce function. 我花了几个小时尝试调试特定的代码,但最后我发现我对arr reduce函数不了解。 Take the below code example
以下面的代码示例
var arr = [{ start: 1, value: 4 }, { start: 2, value: 5 }, { start: 3, value: 1 }, { start: 4, value: 41 }, { start: 5, value: 14 }, { start: 6, value: 3 }]; const getMinValue = function(a, b) { return (a.value < b.value) ? a.start : b.start; } console.log('The min value ', arr.reduce(getMinValue));
The console above returns 6.However, from, the array , its noticeable that the value is minimum at start:3
. 上面的控制台返回6.但是,从数组开始,值得注意的是,该值在
start:3
处为最小值。 Rewriting the code to below however, 但是,将代码重写到下面,
var arr = [{ start: 1, value: 4 }, { start: 2, value: 5 }, { start: 3, value: 1 }, { start: 4, value: 41 }, { start: 5, value: 14 }, { start: 6, value: 3 }]; const getMinValue = function(a, b) { return (a.value < b.value) ? a : b; } console.log('The min value ', arr.reduce(getMinValue));
returns object {start: 3, value: 1}
which is exactly correct. 返回完全正确的对象
{start: 3, value: 1}
。 Hence 因此
console.log('The min value ', arr.reduce(getMinValue).start);
is correct. 是正确的。 Why does the first one differ pls ?
为什么第一个请不同? Why does it return 6 ?
为什么返回6? .
。 Is there something i am mistaking about reduce or my getMin function ?
我是否对reduce或getMin函数有误解? Any help would be appreciated.
任何帮助,将不胜感激。
Why does it return 6 ?
为什么返回6?
because a
is an accumulator
which also happens to be return value of every iteration . 因为
a
是一个accumulator
,也恰好是每次迭代的返回值 。
This line 这条线
return (a.value < b.value) ? a.start : b.start;
translates to conditional expression as 转换为条件表达式为
{start: 1, value : 4}.value < { start: 2, value: 5}.value
and next time onwards 然后下一次
(4).value < { start: 3, value: 1 } //false hence return 1
Since (4).value
is undefined
and comparison with undefined
always returns false
. 由于
(4).value
是undefined
,因此与undefined
比较总是返回false
。
Last iteration will be 最后一次迭代将是
(14).value < { start: 6, value: 3 }
return 6
. 返回
6
。
Because whatever you return from the function you pass into reduce becomes the NEXT value of a
. 因为无论从函数返回的结果是什么,传递给reduce的值都会成为
a
的NEXT值。
const someReducer = (accumulator, x) => {
// whatever I return here is the new value of the accumulator
return accumulator.value < x.value ? accumulator.start : x.start;
}
someArray.reduce(someReducer);
The value of accumulator
on the first time around is an object just like value
. accumulator
的第一次value
就像value
一样是一个对象。 Then you return a number. 然后您返回一个数字。 On the second time around,
accumulator
is now the number you returned. 第二次,
accumulator
现在是您返回的数字。
In general, you should also be using the second argument of reduce
, which is the initial value of the reducer. 通常,您还应该使用
reduce
的第二个参数,它是reducer的初始值。 This is incredibly important if you have situations where your ideal accumulator
type is different than the type of values in your array. 如果您的理想
accumulator
类型不同于数组中值的类型,则这非常重要。
In the case where you have a slightly simpler version you might consider: 如果您的版本稍微简单一些,则可以考虑:
values.reduce(getMinValue, Infinity);
Your initial function would work fine, if it were changed to 如果您将初始功能更改为
const getMinValue = (accumulator, x) =>
accumulator < x.value ? accumulator : x.value;
In this example, accumulator is always a number, and x is one of your objects. 在此示例中,累加器始终是一个数字,而x是您的对象之一。 This isn't quite what you asked for in the original question, but
这与您在原始问题中所要求的完全不同,但是
.reduce(getMinValue, { start: 0, value: Infinity })
would also work. 也可以。
The filter() method returns a new array created from all elements that pass a certain test preformed on an original array. filter()方法返回一个新数组,该数组由所有通过在原始数组上执行的特定测试的元素创建。
var landSum = data.reduce(function(sum, d) {
return sum + d.land_area;
}, 0);
console.log(landSum);
The first parameter to reduce is the callback function that will return the running "total" of the reduction. 要减少的第一个参数是回调函数,该函数将返回减少的运行“总计”。 This function is passed in the previous value returned from the last time the callback was called.
该函数传入上次调用回调时返回的前一个值。 Here, that parameter - sum provides the running total as we move through the array.
在这里,该参数-sum提供了我们在数组中移动时的运行总计。 The second parameter to the callback d is the current value of the array we are working on.
回调d的第二个参数是我们正在处理的数组的当前值。
reduce can take an initial value, which is the second parameter to the reduce call. reduce可以取一个初始值,它是reduce调用的第二个参数。 For this example, we start the sum at 0. If there is no starting value provided, then for the first execution of the callback (when there is no previous value) the first parameter to the callback will be the value of the first element of the array, and the reduction starts with the second element.
在此示例中,我们将总和从0开始。如果未提供起始值,则对于首次执行回调(如果没有先前值),则回调的第一个参数将为的第一个元素的值数组,并且减少从第二个元素开始。
So in you case you are passing the variable as return to the reduce that is why the above results are obtained 因此,在这种情况下,您将变量作为返回值传递给reduce,这就是获得上述结果的原因
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.