javascript.basics icon indicating copy to clipboard operation
javascript.basics copied to clipboard

用reduce计算数组中某个项出现的次数

Open Kelichao opened this issue 5 years ago • 0 comments

// 用reduce时:
    var arr = ["李","李","设","弟","弟","生","生","李"]; 
    function getRepeatNum(){ 
        return arr.reduce(function(prev,next){ 
            prev[next] = (prev[next] + 1) || 1; 
            return prev; 
        },{}); 
    } 
    console.log(getRepeatNum());

Kelichao avatar Jun 30 '20 08:06 Kelichao