簡體   English   中英

有人可以解釋這里發生的步驟流程嗎?

[英]Can someone explain the flow of steps that are happening here?

我試圖弄清楚每個項目是否在 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);

javascript 中的所有常規 - 非生成器 - 函數都具有運行到完成語義。 這意味着當它們被調用時,執行直到它們返回而不會中斷。

在這種情況下, map() function 執行並返回一個數組,然后reduce() function 執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM