Count number of occurrences for each char in a string

后端 未结 11 2324
挽巷
挽巷 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条回答
  •  萌比男神i
    2020-12-16 18:20

    let str = "atul kumar srivastava";
    let obj ={};
    for(let s of str)if(!obj[s])obj[s] = 1;else obj[s] = obj[s]  + 1;
    console.log(obj)
    

提交回复
热议问题