Trying to sum the total of the items selected in number input component

Hello world! I am trying to sum the value [price] of items selected from a multiselect component in a number input component. I'm not too sure how to go about it. Any pointers in the right direction would be greatly appreciated. Many Thanks, Thomas

@webm4ster Welcome to the forum!
Can you share the data structure for the multi-select dropdown?
You may want to sum on the price only but there needs to be a way for the price to stand alone on it's own when the value is selected in the dropdown.

Thank you for responding! The data is just seperate documents in firestore containing 'name' & 'price'. I have attaced a screen shot.

OK so there are a few options here that you can do.
You can substring the value of the label and add each choice to an array in a temp state and then sum the temp state using ._sum()
The reason for this is because when you try to substring on your label, it works, BUT you can't use ._sum because it's a string!
Screenshot 2022-12-20 at 8.33.26 AM
Let me know if you need help to build this out using a temp state.

Hi Scott, thank you for your prompt reponse. A little help may be needed if that is at all possible. Thank you, Thomas

var sumAll = [];
for(i=0;i<multiselect1.selectedLabels.length;i++){
sumAll.push(_.toNumber(multiselect1.selectedLabels[i].substring(multiselect1.selectedLabels[i].indexOf('$') +1)));
}
return _.sum(sumAll)

run addAllChoices when changing the multi-select

and in the bottom field where total cost is displayed use {{addAllChoices.data}}

replace dollar sign with euro

2 Likes

Awesome - Absolute legend. Thank you Scott.