简体   繁体   English

如何在 FOR 循环节点 Js 中使用数组中的值的总和

[英]How to use the sum of values in an array inside FOR loop Node Js

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.我的问题是我想获得具有 RATIO 的 SplitType 的 SplitValue 的总和,并在 FOR 循环中使用它来计算拆分的比率。 ie 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即 3 + 2 = 5。我在下面每一行代码中所做的评论解释了我的麻烦,我得到了 3 和 5 的循环,而不是只有 5。谢谢

 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); } }

This code is simple to understand and modify I hope to your liking.这段代码很容易理解和修改我希望你喜欢。 Using the array method reduce .使用数组方法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)

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM