Adding values in an *ngFor

Hi,

So I have the following values in my loop:

369
369
269

I have a method:

getTotalAmount() {
    if (this.ordersummary) {
      return this.ordersummary.map(t => t.order_cost).reduce((a, value) => a + value, 0);
    }
    return 0;
  }

this outputs: 0369369269

Please help, I need these values to be added

have you tried parseInt on the value?

Try this:

... .reduce((a, value) => a + +value, 0);

Homework: https://www.freecodecamp.org/news/js-type-coercion-explained-27ba3d9a2839/
:raised_hands:

1 Like

is this a string? that would explain what’s going on. Change it to a number, and maybe things will work

1 Like

ah yes, I had to declare it as a number and remove the ‘’ in the model

What are you trying to accomplish? What isn’t working as expected? Is this a problem with reduce or *ngFor?