从数组中获取唯一值并计算数量。重复

时间:2017-05-17 21:51:52

标签: javascript arrays arraylist imacros

从数组中获取值时遇到问题: 我的代码是 -

var arr = [];
for (var k =1; k<=10000; k++)`  
{  
ret = iimSet("k", k);  
      retcode = iimPlay(test)  
  var s = iimGetLastExtract(1);  
if(s == null){  
          break;  
          }  
          else {            
    arr.push(s);  

          }  
var all = arr.join(" , ");  
var final = squash(arr);  
}

所以我能够删除重复项,但所有结果都在一行中。

我真正希望实现的是

     var arr = [];
                for (var k =1; k<=10000; k++)`  
                {  
                ret = iimSet("k", k);  
                      retcode = iimPlay(test)  
                  var s = iimGetLastExtract(1);  
                if(s == null){  
                          break;  
                          }  
                          else {            
                    arr.push(s);  

                          }  
                        var final = squash(arr);  

              only unqiue values
        var unq = no. of total unique values

                }

        for (var h =1; h<=unq; h++)`  
                {  
         var g=h-1;
        var c = every varaible value one by one (only unique value)
       var b = no. of times the value is repeating in the array

        ret = iimSet("c", c);
   ret = iimSet("b", b);  
        iimPlay(Save)
        }

所以基本上我希望逐个数组中的所有值,并且不会出现任何次数。我现在被困住了。

1 个答案:

答案 0 :(得分:0)

我会在对象中存储出现次数,对象的键是有问题的值:

    var obj = {};
    for (var k =1; k<=10000; k++)
    {  
        // these three lines are your code and I dont know what they do.  But `s` is our value
        ret = iimSet("k", k);  
        retcode = iimPlay(test)  
        var s = iimGetLastExtract(1);

        if (!obj[s]) obj[s] = 0;
        obj[s]++;
    }

// the unique elements of your array are in Object.keys(obj)
// the count of occurrences are in obj[element];