简体   繁体   中英

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?

 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM