簡體   English   中英

如何根據其屬性標准計算所有項目的小計:數量*價格?

[英]How to compute subtotal of all items on their property criteria: qty * price?

我有一個表示客戶購買的商品的數組,並按以下邏輯計算商品小計:

function getSubtotal(arr) {
  let total = 0.00;
  arr.forEach(function(i) {
    total += (i.qty * i.price)
  });
  return total;  // 1274.21
}

是的,當然可以,我得到了正確的結果,但是有沒有有效的方法可以在javascript中做到這一點?

 let data = [ { name : 'Item 1', qty: 2, price: 15.50 // 31 }, { name : 'Item 2', qty: 17, price: 25.13 // 427.21 }, { name : 'Item 3', qty: 102, price: 8.00 // 816 } ]; function getSubtotal(arr) { let total = 0.00; arr.forEach(function(i) { total += (i.qty * i.price) }); return total; // 1274.21 } document.write(getSubtotal(data)); 

這是使用reduce的一種簡單方法。

function getSubtotal (arr) {
  return arr.reduce((total, obj) => {
    return total + obj.qty * obj.price;
  }, 0);
}

可以這樣寫

function getSubtotal (arr) {
  return arr.reduce((total, obj) => total + obj.qty * obj.price, 0);
}

要么

return arr.reduce((total, {qty, price}) => total + qty * price, 0);

我想編輯@FrankCamara的答案,但不能。 所以這里是不同的格式:


這是使用reduce的一種簡單方法。

 let data = [ { name : 'Item 1', qty: 2, price: 15.50 // 31 }, { name : 'Item 2', qty: 17, price: 25.13 // 427.21 }, { name : 'Item 3', qty: 102, price: 8.00 // 816 } ]; function getSubTotal (items) { const fn = (total, { price, qty }) => total + price * qty; return items.reduce(fn, 0); } console.log(getSubTotal(data)); 

這是使用map另一種解決方案

var Data = [
  {name : 'Item 1', qty: 2, price: 15.50},
  {name : 'Item 2', qty: 17, price: 25.13}, 
  {name : 'Item 3', qty: 102, price: 8.00}
];

var total = 0;
var reformattedArray = Data.map(function(obj) {
   total+=obj.qty*obj.price;
   return total;
});

console.log(total); /*Sum of all qty*price`*/
console.log(reformattedArray); /*Sum of its and previous index as an array*/
console.log(reformattedArray[reformattedArray.length-1]); /*SubTotal, Last Index*/

console.log(total) 結果

1274.21

console.log(reformattedArray) 結果

[
  31,       /* (2*15.50) */
  458.21,   /* (2*15.50)+(17*25.13) */
  1274.21   /* (2*15.50)+(17*25.13)+(102*8.00) */
]

結果 console.log(reformattedArray[reformattedArray.length-1]) ;

1274.21

似乎for循環比forEachreduce更快。 這是一個測試演示。

 let data = [{ name: 'Item 1', qty: 2, price: 15.50 // 31 }, { name: 'Item 2', qty: 17, price: 25.13 // 427.21 }, { name: 'Item 3', qty: 102, price: 8.00 // 816 }]; function getSubtotalReduce(arr) { return arr.reduce((total, item) => total + item.qty * item.price, 0.00); } console.time('reduce'); console.log(getSubtotalReduce(data)); console.timeEnd('reduce'); function getSubtotal(arr) { let total = 0.00; arr.forEach(function(i) { total += (i.qty * i.price) }); return total; // 1274.21 } console.time('forEach'); console.log(getSubtotal(data)); console.timeEnd('forEach'); function getTotalFor(data) { var total = 0; for (var i = 0, len = data.length; i < len; i++) { total += data[i].qty * data[i].price; } return total; } console.time('for'); console.log(getTotalFor(data)); console.timeEnd('for'); 

資料來源: CodeReview

暫無
暫無

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

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