Each Character occurrence in a string

前端 未结 3 1130
不知归路
不知归路 2021-01-17 05:12

How to write a javascript code that counts each character occurrence in a string ?

e.g 
String is : Hello World 

Output :  
count of H -> 1
count of e -&         


        
3条回答
  •  时光取名叫无心
    2021-01-17 05:26

    this code should work:

    var str = "Hello World";
    var arr = str.split('');
    var occ = {};
    for(var i=0,c=arr.length;i '+occ[i]); 
    }
    

提交回复
热议问题