[英]Can someone explain the flow of steps that are happening here?
I'm trying to figure out is each item going through reduce as the map runs, or is the map method running completely, and then after it returns the array, the reduce method is being applied to the array from the map?我试图弄清楚每个项目是否在 map 运行时通过 reduce,或者 map 方法是否完全运行,然后在它返回数组后,reduce 方法正在从 Z1D78DC8ED51214E518B5114FEZ2 应用于数组?
const lineItems = [
{ description: 'Eggs (Dozen)', quantity: 1, price: 3, total: 3 },
{ description: 'Cheese', quantity: 0.5, price: 5, total: 2.5 },
{ description: 'Butter', quantity: 2, price: 6, total: 12 }
];
let store = lineItems.map(item => item.quantity).reduce(sumReducer, 0);
function sumReducer(sum, val) {
return sum += val;
}
console.log(store);
All regular - non generator - functions in javascript have run-to-completion semantics. javascript 中的所有常规 - 非生成器 - 函数都具有运行到完成语义。 This means that when they are called, the execute until they return without interruption.这意味着当它们被调用时,执行直到它们返回而不会中断。
In this case the map() function executes and returns an array, then the reduce() function executes.在这种情况下, map() function 执行并返回一个数组,然后reduce() function 执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.