Javascript 如何计算每个条件下的循环数并将其存储到数组中?

Javascript 如何计算每个条件下的循环数并将其存储到数组中?,javascript,arrays,loops,Javascript,Arrays,Loops,我对..使用循环对一些值求和,并将其存储到具有特定条件的数组中,但现在我想计算循环已求和的项数。 e、 g.我在某些提示中有5项:1、4、6、2、7,我用循环求和,所以我可以得到20,但我如何知道循环求和了多少项(在本例中是5项)。 以下是我迄今为止所做的: 这是我的循环: var SumByTipe = {}; for(i in data.GetReportIdResult){ var currtipe = data.GetReportIdResult[i].tipe; if

我对..使用循环
对一些值求和,并将其存储到具有特定条件的数组中,但现在我想计算循环已求和的项数。
e、 g.我在某些
提示中有5项:1、4、6、2、7,我用循环求和,所以我可以得到20,但我如何知道循环求和了多少项(在本例中是5项)。
以下是我迄今为止所做的:

这是我的循环:

var SumByTipe = {};

for(i in data.GetReportIdResult){
    var currtipe = data.GetReportIdResult[i].tipe;
    if (currtipe in SumByTipe){
        for (j in data.GetReportIdResult[i]){
            if (j != "tipe" && j != "mc"){
                SumByTipe[currtipe][j + '_total'] += parseFloat(data.GetReportIdResult[i][j]);
                SumByTipe[currtipe]['isi'] += 1;  //---I want to count how many items in every tipe (e.g. in ESL there are 5 items)---//                  
            }
        }
    }else{
        var firstSum = {};
        for (j in data.GetReportIdResult[i]){
            if (j != "tipe" && j != "mc"){
                firstSum[j + '_total'] = parseFloat(data.GetReportIdResult[i][j]);
            }
        }
        SumByTipe[currtipe]=firstSum;
    }
}

alert(SumByTipe['ESL']['isi']);
这是我在json数组中的项:

var data = {
  "GetReportIdResult": [
    {
      "act_qty1": "834",
      "act_val1": "11",
      "acvqty": "0.99",
      "acvval": "0.84",
      "budqty": "84332",
      "budval": "1315",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "21.9",
      "cm_per2": "31.8",
      "cm_per3": "29.2",
      "cm_val1": "2",
      "cm_val2": "206",
      "cm_val3": "356",
      "mc": "Agen",
      "tipe": "ESL"
    },
    {
      "act_qty1": "17076",
      "act_val1": "186",
      "acvqty": "36.01",
      "acvval": "38.64",
      "budqty": "47416",
      "budval": "482",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "8.6",
      "cm_per2": "7.0",
      "cm_per3": "8.1",
      "cm_val1": "16",
      "cm_val2": "47",
      "cm_val3": "54",
      "mc": "Hotel & Restaurant",
      "tipe": "ESL"
    },
    {
      "act_qty1": "30948",
      "act_val1": "299",
      "acvqty": "16.48",
      "acvval": "16.22",
      "budqty": "187788",
      "budval": "1841",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "-3.2",
      "cm_per2": "-3.1",
      "cm_per3": "-3.1",
      "cm_val1": "-10",
      "cm_val2": "-61",
      "cm_val3": "-55",
      "mc": "Institution",
      "tipe": "ESL"
    },
    {
      "act_qty1": "8358",
      "act_val1": "121",
      "acvqty": "37.96",
      "acvval": "35.26",
      "budqty": "22019",
      "budval": "342",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "26.7",
      "cm_per2": "28.4",
      "cm_per3": "30.1",
      "cm_val1": "32",
      "cm_val2": "128",
      "cm_val3": "155",
      "mc": "Lower Trade",
      "tipe": "ESL"
    },
    {
      "act_qty1": "53433",
      "act_val1": "943",
      "acvqty": "19.45",
      "acvval": "19.48",
      "budqty": "274742",
      "budval": "4844",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "31.2",
      "cm_per2": "31.2",
      "cm_per3": "31.2",
      "cm_val1": "295",
      "cm_val2": "1423",
      "cm_val3": "1567",
      "mc": "Modern Market",
      "tipe": "ESL"
    },
    {
      "act_qty1": "0",
      "act_val1": "0",
      "acvqty": "0.00",
      "acvval": "0.00",
      "budqty": "732",
      "budval": "13",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "0.0",
      "cm_per2": "34.5",
      "cm_per3": "34.8",
      "cm_val1": "0",
      "cm_val2": "9",
      "cm_val3": "15",
      "mc": "Agen",
      "tipe": "ESL500ML"
    },
    {
      "act_qty1": "156",
      "act_val1": "3",
      "acvqty": "0.00",
      "acvval": "0.00",
      "budqty": "0",
      "budval": "0",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "33.0",
      "cm_per2": "33.2",
      "cm_per3": "33.3",
      "cm_val1": "1",
      "cm_val2": "2",
      "cm_val3": "4",
      "mc": "Hotel & Restaurant",
      "tipe": "ESL500ML"
    },
    {
      "act_qty1": "90",
      "act_val1": "2",
      "acvqty": "20.11",
      "acvval": "21.33",
      "budqty": "448",
      "budval": "8",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "39.4",
      "cm_per2": "32.8",
      "cm_per3": "37.8",
      "cm_val1": "1",
      "cm_val2": "1",
      "cm_val3": "2",
      "mc": "Lower Trade",
      "tipe": "ESL500ML"
    },
    {
      "act_qty1": "3535",
      "act_val1": "68",
      "acvqty": "20.57",
      "acvval": "20.63",
      "budqty": "17189",
      "budval": "331",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "39.7",
      "cm_per2": "40.8",
      "cm_per3": "40.3",
      "cm_val1": "27",
      "cm_val2": "140",
      "cm_val3": "166",
      "mc": "Modern Market",
      "tipe": "ESL500ML"
    },
    {
      "act_qty1": "2700",
      "act_val1": "29",
      "acvqty": "2.84",
      "acvval": "3.27",
      "budqty": "95040",
      "budval": "899",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "9.5",
      "cm_per2": "11.1",
      "cm_per3": "12.9",
      "cm_val1": "3",
      "cm_val2": "76",
      "cm_val3": "74",
      "mc": "Agen",
      "tipe": "UHT"
    },
    {
      "act_qty1": "3216",
      "act_val1": "35",
      "acvqty": "18.93",
      "acvval": "19.03",
      "budqty": "16993",
      "budval": "184",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "10.8",
      "cm_per2": "7.6",
      "cm_per3": "6.6",
      "cm_val1": "4",
      "cm_val2": "9",
      "cm_val3": "9",
      "mc": "Hotel & Restaurant",
      "tipe": "UHT"
    },
    {
      "act_qty1": "10701",
      "act_val1": "105",
      "acvqty": "23.85",
      "acvval": "24.15",
      "budqty": "44872",
      "budval": "434",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "1.4",
      "cm_per2": "-0.6",
      "cm_per3": "-0.5",
      "cm_val1": "1",
      "cm_val2": "-3",
      "cm_val3": "-2",
      "mc": "Institution",
      "tipe": "UHT"
    },
    {
      "act_qty1": "5663",
      "act_val1": "65",
      "acvqty": "18.58",
      "acvval": "19.33",
      "budqty": "30487",
      "budval": "334",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "12.7",
      "cm_per2": "12.0",
      "cm_per3": "11.2",
      "cm_val1": "8",
      "cm_val2": "36",
      "cm_val3": "40",
      "mc": "Lower Trade",
      "tipe": "UHT"
    },
    {
      "act_qty1": "16637",
      "act_val1": "197",
      "acvqty": "18.60",
      "acvval": "18.38",
      "budqty": "89424",
      "budval": "1072",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "9.3",
      "cm_per2": "7.4",
      "cm_per3": "11.8",
      "cm_val1": "18",
      "cm_val2": "68",
      "cm_val3": "133",
      "mc": "Modern Market",
      "tipe": "UHT"
    },
    {
      "act_qty1": "84",
      "act_val1": "3",
      "acvqty": "2.10",
      "acvval": "2.10",
      "budqty": "4008",
      "budval": "132",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "50.9",
      "cm_per2": "52.9",
      "cm_per3": "51.0",
      "cm_val1": "1",
      "cm_val2": "3",
      "cm_val3": "110",
      "mc": "Agen",
      "tipe": "WHP"
    },
    {
      "act_qty1": "948",
      "act_val1": "30",
      "acvqty": "26.33",
      "acvval": "27.82",
      "budqty": "3600",
      "budval": "108",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "48.9",
      "cm_per2": "50.5",
      "cm_per3": "41.2",
      "cm_val1": "15",
      "cm_val2": "63",
      "cm_val3": "76",
      "mc": "Hotel & Restaurant",
      "tipe": "WHP"
    },
    {
      "act_qty1": "2604",
      "act_val1": "71",
      "acvqty": "9.18",
      "acvval": "9.28",
      "budqty": "28360",
      "budval": "769",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "43.3",
      "cm_per2": "42.9",
      "cm_per3": "39.3",
      "cm_val1": "31",
      "cm_val2": "233",
      "cm_val3": "358",
      "mc": "Institution",
      "tipe": "WHP"
    },
    {
      "act_qty1": "6",
      "act_val1": "0",
      "acvqty": "3.33",
      "acvval": "4.26",
      "budqty": "180",
      "budval": "6",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "60.4",
      "cm_per2": "0.0",
      "cm_per3": "0.0",
      "cm_val1": "0",
      "cm_val2": "0",
      "cm_val3": "0",
      "mc": "Lower Trade",
      "tipe": "WHP"
    },
    {
      "act_qty1": "266",
      "act_val1": "11",
      "acvqty": "50.38",
      "acvval": "50.51",
      "budqty": "528",
      "budval": "21",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "48.6",
      "cm_per2": "48.8",
      "cm_per3": "49.2",
      "cm_val1": "5",
      "cm_val2": "14",
      "cm_val3": "17",
      "mc": "Modern Market",
      "tipe": "WHP"
    },
    {
      "act_qty1": "0",
      "act_val1": "0",
      "acvqty": "0.00",
      "acvval": "0.00",
      "budqty": "0",
      "budval": "0",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "0.0",
      "cm_per2": "5.1",
      "cm_per3": "5.5",
      "cm_val1": "0",
      "cm_val2": "9",
      "cm_val3": "6",
      "mc": "Agen",
      "tipe": "CHEESE1K"
    },
    {
      "act_qty1": "127",
      "act_val1": "8",
      "acvqty": "0.98",
      "acvval": "0.98",
      "budqty": "13000",
      "budval": "845",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "21.2",
      "cm_per2": "21.7",
      "cm_per3": "22.8",
      "cm_val1": "2",
      "cm_val2": "33",
      "cm_val3": "40",
      "mc": "Hotel & Restaurant",
      "tipe": "CHEESE1K"
    },
    {
      "act_qty1": "0",
      "act_val1": "0",
      "acvqty": "0.00",
      "acvval": "0.00",
      "budqty": "0",
      "budval": "0",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "0.0",
      "cm_per2": "19.8",
      "cm_per3": "0.0",
      "cm_val1": "0",
      "cm_val2": "0",
      "cm_val3": "0",
      "mc": "Lower Trade",
      "tipe": "CHEESE1K"
    },
    {
      "act_qty1": "0",
      "act_val1": "0",
      "acvqty": "0.00",
      "acvval": "0.00",
      "budqty": "0",
      "budval": "0",
      "bulan1": "7 ",
      "bulan2": "6 ",
      "bulan3": "5 ",
      "cm_per1": "0.0",
      "cm_per2": "19.0",
      "cm_per3": "0.0",
      "cm_val1": "0",
      "cm_val2": "0",
      "cm_val3": "0",
      "mc": "Modern Market",
      "tipe": "CHEESE1K"
    }
  ]
};
var-SumByTipe={};
对于(data.GetReportIdResult中的i){
var currtipe=data.GetReportIdResult[i].tipe;
SumByTipe[currtipe]=0;
if(SumByTipe中的货币){

//我希望你能从中得到你可能需要的;)

你做得很正确,只是在循环开始时,你需要初始化
SumByTipe[currtipe]['isi']
0
…如下:@amnotiam足够接近,但结果是错误的,它应该是5,就像在我的数组中,
tipe
ESL有5个
mc
(代理、机构、低档贸易、现代市场和酒店和餐厅)。因此,它应该显示5,但您的答案显示15。但非常感谢您的帮助:)但我想将每个
tipe
中的计数结果放入数组中,例如在
tipe
ESL中有5个
mc
(代理、机构、低级贸易、现代市场以及酒店和餐厅).因此,如果我想知道每个
tipe
中有多少
mc
,我可以像CountResult['ESL',CountResult['UHT']那样调用它们,等等..这里使用的JSFIDLE更新是您提供的正确更新,它显示了,但似乎在每个
tipe
中都要计算
tipe
而不是
mc
。如果我对我的问题的解释不好,我很抱歉,但感谢您的帮助。也许这是我的意思,但它计算了错误的项目,它计算了多少在
ESL
中的项目(显示16个),我需要的是计算
ESL
中的
mc
(结果应该是5个
mc
s(代理、机构、低档贸易、现代市场、酒店和餐厅)。这就是我需要的。谢谢我的朋友,这非常有用:D这里是我的更新
var SumByTipe = {};

for(i in data.GetReportIdResult){
    var currtipe = data.GetReportIdResult[i].tipe;
    SumByTipe[currtipe] = 0;
    if (currtipe in SumByTipe){
         // <<<< RIGHT HERE <<<<<<
        for (j in data.GetReportIdResult[i]){
            if (j != "tipe" && j != "mc"){
                SumByTipe[currtipe][j + '_total'] += parseFloat(data.GetReportIdResult[i][j]);

                //SumByTipe[currtipe]['isi'] += 1;  //---I want to count how many items in every tipe (e.g. in ESL there are 5 items)---//                  
            }
        }
    }else{
        var firstSum = {};
        for (j in data.GetReportIdResult[i]){
            if (j != "tipe" && j != "mc"){
                firstSum[j + '_total'] = parseFloat(data.GetReportIdResult[i][j]);
            }
        }
        //SumByTipe[currtipe]=firstSum;
    }
}
var keys =  Object.keys(SumByTipe);
alert(keys.length);