1

i'm trying to get the total of all my products in my list.

this.totalValue = this.items.filter((item) => item.qtyvalue)
                            .map((item) => item.qtyvalue)
                            .reduce((sum, current) => sum + current)

This is almost working as it gives me 650.00110.0030175.0050.00

But i want the numbers added together, how do i do this?

kind regards

1
  • Try this this.totalValue = this.items.reduce((sum, item) => sum + +item.qtyvalue, 0) Commented Jan 12, 2018 at 13:23

2 Answers 2

4

It's looks like your quantity is string not a number.You can try following code snippet

this.totalValue = this.items.filter((item) =>item.qtyvalue)
                            .map((item) => +item.qtyvalue)
                            .reduce((sum, current) => sum + current);
console.log(this.totalValue);

WORKING DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

The little + sign before item.qtyvalue made the whole thing work... i though i had tried every thing. Thanks man
0

I think, your qtyvalue-s are strings, not numbers, so you get just them concatenated as strings. In the map function use parseFloat.

this.totalValue = this.items.filter(item => item.qtyvalue)
                            .map(item => parseFloat(item.qtyvalue))
                            .reduce((sum, current) => sum + current)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.