How to count duplicate value in an array in javascript

前端 未结 28 1426
后悔当初
后悔当初 2020-11-22 06:07

Currently, I got an array like that:

var uniqueCount = Array();

After a few steps, my array looks like that:

uniqueCount =          


        
28条回答
  •  被撕碎了的回忆
    2020-11-22 06:28

    var string = ['a','a','b','c','c','c','c','c','a','a','a'];
    
    function stringCompress(string){
    
    var obj = {},str = "";
    string.forEach(function(i) { 
      obj[i] = (obj[i]||0) + 1;
    });
    
    for(var key in obj){
      str += (key+obj[key]);
    }
      console.log(obj);
      console.log(str);
    }stringCompress(string)
    
    /*
    Always open to improvement ,please share 
    */

提交回复
热议问题