如何检查数组中的重复名称?

时间:2011-08-17 03:59:22

标签: javascript

我有两个包含不同值的数组作为国家/地区名称及其在数据库中的计数。

China3
Argentina3
Philippines2
Taiwan; Republic of China (ROC)2
Taiwan; Republic of China (ROC)2
Singapore2
Taiwan; Republic of China (ROC)2
Japan2
Singapore2
United States2
United States2
Chile2
Brazil2
Argentina2
Norway2
South Africa2
Dominican Republic2
United States2
Korea Republic of2
Lithuania2
China2
Israel2
Taiwan; Republic of China (ROC)2
Taiwan; Republic of China (ROC)2
Malaysia2
Latvia2
Hungary2
Israel2
Romania2
Italy2
Hungary2
Netherlands2
Russian Federation2
Singapore1
China1

我想检查重复的名称,并使用此代码添加其计数值

      var i ,rowcount;
      var suq = 0;
      var row =0;
      var check = 0;
      var number = 1;

        for (i = 0; i<count; i++){

        var countryname = countryarray[i];
        var hostcount = hosthitcount[i];

           for (rowcount=0;rowcount<number;rowcount++){
               //check for any repeated country name

             if (data.getValue(rowcount,0) == countryname ){

                           //get the hits                //rowindex
                  var pastvalue = data.getValue(rowcount,1);

                  //add the previous value with current value
                  var value = parseInt(hostcount)+parseInt(pastvalue);
                  value+= "";

                  //put it in the table
                  data.setValue(rowcount,1,value);
                  row++


             }else{
        data.setValue(suq, 0, countryname);
        data.setValue(suq, 1, hostcount);
        check++


        suq++;
             }//end of else

        }//second for loop
           }//first for loop

并且显示的结果只是我的数组中的第一个值已加起来但另一个不是 这是结果=

China6
Argentina3
Philippines2
Taiwan; Republic of China (ROC)2
Taiwan; Republic of China (ROC)2
Singapore2
Taiwan; Republic of China (ROC)2
Japan2
Singapore2
United States2
United States2
Chile2
Brazil2
Argentina2
Norway2
South Africa2
Dominican Republic2
United States2
Korea Republic of2
Lithuania2
Israel2
Taiwan; Republic of China (ROC)2
Taiwan; Republic of China (ROC)2
Malaysia2
Latvia2
Hungary2
Israel2
Romania2
Italy2
Hungary2
Netherlands2
Russian Federation2
Singapore1
nullnull
nullnull

提前感谢。

1 个答案:

答案 0 :(得分:0)

问题不是很清楚,我只能看到一个数组,除了我建议你在一个单独的函数中提取解析和计算国家名后面的数字,所以你已经分开搜索并查找函数和解析并计算一个。

如果不理解你的代码逻辑,这一行看起来真的很奇怪,因为只有一个循环被执行,只引用了第一个国家:

for (rowcount=0;rowcount<number;rowcount++)

号码设为1