How to count duplicate value in an array in javascript

前端 未结 28 1408
后悔当初
后悔当初 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:21

    Something like this:

    uniqueCount = ["a","b","c","d","d","e","a","b","c","f","g","h","h","h","e","a"];
    var count = {};
    uniqueCount.forEach(function(i) { count[i] = (count[i]||0) + 1;});
    console.log(count);

    Use a simple for loop instead of forEach if you don't want this to break in older browsers.

提交回复
热议问题