Count the number of occurrences of a character in a string in Javascript

后端 未结 30 2483
礼貌的吻别
礼貌的吻别 2020-11-22 02:33

I need to count the number of occurrences of a character in a string.

For example, suppose my string contains:

var mainStr = \"str1,str2,str3,str4\";         


        
30条回答
  •  伪装坚强ぢ
    2020-11-22 03:09

    My solution with ramda js:

    const testString = 'somestringtotest'
    
    const countLetters = R.compose(
      R.map(R.length),
      R.groupBy(R.identity),
      R.split('')
    )
    
    countLetters(testString)
    

    Link to REPL.

提交回复
热议问题