Count number of occurrences for each char in a string

后端 未结 11 2224
挽巷
挽巷 2020-12-16 17:42

I want to count the number of occurrences of each character in a given string using JavaScript.

For example:

var str = \"I want to count the number         


        
11条回答
  •  时光说笑
    2020-12-16 18:02

     // Converts String To Array
            var SampleString= Array.from("saleem");
    
            // return Distinct count as a object
            var allcount = _.countBy(SampleString, function (num) {
                return num;
            });
    
            // Iterating over object and printing key and value
            _.map(allcount, function(cnt,key){
                console.log(key +":"+cnt);
            });
    
            // Printing Object
            console.log(allcount);
    
    
        

    Set the variable to different value and then try...

提交回复
热议问题