I have a list that I get from an axios call, what I try is to create an array where the products that have the same id, increase the amount and subtotal for that product, otherwise they will be added with the amount as it is.
but I always get quantity = 1.xx and a wrong total, I do not understand why or where the fault can be
let dataGroup = [];
resp.data.forEach((element)=>{
let indexElement = dataGroup.findIndex(elm => {
return (elm.product_id === element.product_id ) && element.print;
});
if(indexElement !== -1){
dataGroup[indexElement].total += parseFloat(element.total);
dataGroup[indexElement].count += parseInt(element.count);
roundNumber(dataGroup[indexElement].count,0);
roundNumber(dataGroup[indexElement].total,2);
}
else{
dataGroup.push(element);
}
});
Function
window.roundNumber = function(num, scale) {
if(!("" + num).includes("e")) {
return +(Math.round(num + "e+" + scale) + "e-" + scale);
} else {
var arr = ("" + num).split("e");
var sig = ""
if(+arr[1] + scale > 0) {
sig = "+";
}
return +(Math.round(+arr[0] + "e" + sig + (+arr[1] + scale)) + "e-" + scale);
}
}
dataGroup in the first instance is empty, everything works according to the data I receive from my call with axios, for example in the image the first row I must get 9 in quantity but I receive that 1.00, the unit value is 35 should be 9 * 53 in ST, but it's not like that
Response for axios
[
{
"id": 4,
"product_id": 1,
"count": "1.00",
"total": "35.00",
"print": 1
},
{
"id": 5,
"product_id": 1,
"count": "1.00",
"total": "35.00",
"print": 1
},
{
"id": 6,
"product_id": 1,
"count": "1.00",
"total": "35.00",
"print": 1
}
....

dataGrouparray,element.print, etc.?resp.datademo data?