Total Up all amount array + delivery charges

hello, i would like to know how to create a subtotal price of an array + a delivery fee which is not inside the array

i tried this but the delivery fee is rejected, how to include this delivery fee into the subtotal?

at the TS

deliveryfee=// this comes from another function which has no issue at show the fee at ngoninit and ionviewdidenter

subtotaltest(arr) {
let total = 0;
arr.forEach(element => {
total += parseFloat[(element.price) * element.quantity] + this.deliveryfee;
return total


Sub Total: {{ subtotaltest(product) | currency : ‘USD’}}

thank you very much for your guidance

Place some console logs on delivery fee. E.g. in the fubction. If it doesnt show, the issue is outside the code u r displaying

hi, I did console log on the delivery fee, but it shows NaN

i tried
total += (element.price) * element.quantity + this.deliveryfee;

the VS code tells me
Operator ‘+’ cannot be applied to types ‘number’ and ‘any’.


If it is NaN then u cant use it to count

So your problem is where u assign value to it

Besides u may have given it a wrong type


that means i need to figure out
i tried this but not successful too it came out like this.

total += parseFloat[(element.price) * element.quantity] + [this.deliveryfee];

is there a better way for me to add up the array amount + deliver fees?

subtotaltest(arr) {
let total = 0;
arr.forEach(element => {
total += parseFloat(element.price * element.quantity) + this.deliveryfee;
return total

You are also not using parseFloat properly… (was difficult to read from phone)

Secondly, maybe good to see what type the data in this.deliveryfee.

So maybe do this to debug multiple aspects of what is going on?

subtotaltest(arr) {
console.log('step 1',typeof this.deliveryfee, this.deliveryfee);
let total = 0;
this.deliveryfee = 100;
console.log('step 2',typeof this.deliveryfee, this.deliveryfee); // should show number, 100
arr.forEach(element => {
console.log('Step 3',typeof element.price, typeof element.quantity);
total += parseFloat(element.price * element.quantity) + this.deliveryfee;
return total

Thank you very much for your reply. I give it a try.

Thanks. Will post for more questions soon.

you could try the Array.reduce() method as well, its a built-in method to do just this

MDN | Array.reduce()

Hi. Ok. I will test test using reduce method. :slight_smile:

the reduce method works, but when i try to push subtotal into array, it went crazy, i push 1 it becomes 2, i push 2 it becomes 3, so the correct figure i cannot get it.

i found a solution by combining 2 objects into 1 and it does not have the auto multiplication

the delivery charges and subtotal and total all works perfect now

:slightly_smiling_face: :pray:

Sorry, yeah I think what I was thinking requires you to use to get subtotals first.

//create a new array of items of the totals for each item
//reduce the new array to the sum of the values, 
//with a starting value of you delivery fee
let total = cartArray
    .map((element) => element.price * element.quantity)
    .reduce((acc, curr) => { return acc + curr}, deliveryFee);

i found a solution by combining 2 objects into 1

is this what you ended up with?

morning, i ended up

in my separate post, i yet know how to push data outside the foreach, so i combine the shipping cost and products data to this.deliveryfee,

getTotal() {
return this.deliveryfee.reduce((a, b) => a + b.price * b.quantity + b.shippingcost, 0);

i try to splice the cart and push shipping cost and see see.

thank you for your help