Home > database >  How to use the sum of values in an array inside FOR loop Node Js
How to use the sum of values in an array inside FOR loop Node Js

Time:07-20

My problem is that I want to get the total sum of SplitValue that has the SplitType of RATIO and use it inside the FOR loop to calculate the ratio of split. i.e. 3 2 = 5. The comments I made in each line of my code below explain my troubles, I'm getting a loop of 3 and 5 instead of just 5. Thanks

var details = {
  "Amount": 1396.8000000000002,
  "SplitInfo": [{
      "SplitType": "RATIO",
      "SplitValue": 3
    },
    {
      "SplitType": "RATIO",
      "SplitValue": 2
    }
  ]
};

var conRatioSumArr = 0;
var balance = details.Amount;

for (var i = 0; i < details.SplitInfo.length; i  ) {

  // I want to Get the total sum of SplitValue that has the SplitType of RATIO
  // 3   2 = 5
  // Here is what I've tried

  splitTypeArr = details.SplitInfo[i].SplitType;
  splitValueArr = details.SplitInfo[i].SplitValue;

  if (splitTypeArr === "RATIO") {

    conRatioSumArr  = splitValueArr;
    console.log(conRatioSumArr); // This gives me a loop of 3 & 5. I only need the total value which is 5 instead of both 3 and 5. Note that if I put this outside the for loop, I get only the total which is 5, but I want to use the sum inside of this for loop not outside the for loop to enable me calculate the ratio below.

    splitAmount = (balance * (splitValueArr / 5)); // The total above is expected to replace the 5 here, but I'm getting a loop of 3 & 5 instead.

    // Like this
    // splitAmount = (balance * (splitValueArr / conRatioSumArr));

    // splitAmount is expected to give: 838.08 and 558.7200000000001 split respectively
    console.log(splitAmount);

  }

}

CodePudding user response:

I think you are making it too complicated.

All you need to do is get the total amount of parts in the ratio, and then do the (simple) math.

var details = {
    amount: 1396.8000000000002,
    splitInfo: [
        {
            type: "RATIO",
            value: 3,
        },
        {
            type: "RATIO",
            value: 2,
        },
    ],
};

let totalParts = 0;

for (const split of details.splitInfo) {
    if (split.type == "RATIO") totalParts  = split.value;
}

for (const split of details.splitInfo) {
    if (split.type == "RATIO") {
        let amountForSplit = details.amount * (split.value / totalParts);
        console.log(amountForSplit);
    }
}

CodePudding user response:

This code is simple to understand and modify I hope to your liking. Using the array method reduce.

var details = {
  "Amount": 1396.8000000000002,
  "SplitInfo": [{
      "SplitType": "RATIO",
      "SplitValue": 3
    },
    {
      "SplitType": "RATIO",
      "SplitValue": 2
    }
  ]
};

var result = details.SplitInfo.reduce(function(agg, item) {
  if (item.SplitType == "RATIO") {
    agg.sum = (agg.sum || 0)   item.SplitValue
    agg.count = (agg.count || 0)   1
  }
  return agg;
}, {})

console.log(result)
console.log("average is "   result.sum / result.count)

  • Related