I have an array of called orders,
const orders = [
{
name: 'iPhone 7',
price: 7199,
count: 2
},
{
name: 'iPad 2',
price: 3399,
count: 1
},
{
name: 'Macbook Pro',
price: 19888,
count: 1
},
{
name: 'Kindle Osis',
price: 2399,
count: 2
}
];
and I want to calculate the total price by summing each item's price times count up like this:
orders.reduce((e1, e2) => e1.price * e1.count + e2.price * e2.count);
but I got NaN instead of the total price, anyone can help? Thanks!
You
0 as the initial value of a sumSo use
orders.reduce((sum, el) => sum + el.price * el.count, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With