How to count duplicate value in an array in javascript

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

    You can solve it without using any for/while loops ou forEach.

    function myCounter(inputWords) {        
        return inputWords.reduce( (countWords, word) => {
            countWords[word] = ++countWords[word] || 1;
            return countWords;
        }, {});
    }
    

    Hope it helps you!

提交回复
热议问题