1

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

enter image description here

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
    }
 ....
4
  • Can you provide a demo in JSFiddle or else? provide demo data of dataGroup array, element.print, etc.? Commented Nov 14, 2018 at 17:49
  • @SajibKhan 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 Commented Nov 14, 2018 at 17:56
  • can you provide resp.data demo data? Commented Nov 14, 2018 at 18:02
  • @SajibKhan I added a base example of what the axios request gets Commented Nov 14, 2018 at 18:06

2 Answers 2

1

Here you have some logical issues. Check this (see comments in code):

resp.data.forEach((element)=>{
  let indexElement  = dataGroup.findIndex(elm => {
    return (elm.product_id === element.product_id ) && element.print;
  });
  if(indexElement !== -1){
    // first convert total/count to float/int then update in existing array element
    dataGroup[indexElement].total += roundNumber(parseFloat(element.total), 0);
    dataGroup[indexElement].count += roundNumber(parseInt(element.count), 2);
  }
  else {
    // need to convert total/count to float/int then push to dataGroup
    element.total = roundNumber(parseFloat(element.total), 0);
    element.count = roundNumber(parseInt(element.count), 2);

    dataGroup.push(element);
  }
});

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

1 Comment

Yes, it was that, what a mistake! Thank you
0

It's because of the way you're initializing the items in the group. String values are returned from the server and then you're trying to add them to floats. Try running this through your console.

"35.00" + parseFloat("35.00")

To solve this issue, you must first initialize the value as a float. Instead of this:

dataGroup.push(element)

Try this:

dataGroup.push({
        id: element.id,
        product_id: element.product_id,
        total: parseInt(element.total),
        count: parseFloat(element.count),
        print: !!element.print,
});

Check out this fiddle

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.