[英]Combine a Reduce and Map Data Structure in Javascript?
I am trying to use the reduce pattern on a Javascript Array of objects. 我试图在对象的Javascript数组上使用reduce模式。 But, instead of doing this in a single reducer, I want to be able to use a different reducer based on a condition set for each of the items in the array.
但是,我希望不能够在单个化简器中执行此操作,而是希望能够根据为数组中每个项目设置的条件使用不同的化简器。 So, it would look like the following.
因此,它看起来像下面的样子。
const FOO_MAP = new Map([
["Foo1", Bar1],
["Foo2", Bar2]]
);
function calcTotal(values) {
if(values == null) return 0;
return values.reduce(FOO_MAP.get(value.field));
}
function Bar1(previous, curr){...}
function Bar2(previous, curr){...}
Is this possible? 这可能吗? Thanks!
谢谢!
您似乎在寻找
values.reduce((acc, el, i) => FOO_MAP.get(el.field)(acc, el, i), 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.