counting frequency of characters in a string using javascript -


i need write kind of loop can count frequency of each letter in string.

for example: "aabsssd"

output: a:2, b:1, s:3, d:1

also want map same character property name in object. idea how this?

i not sure how it.

this far:

var arr = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];  function counter(x) {     var count=0, temp = [];     x = x.split('');     console.log(x);     for(var i=0, len = x.length; < len; i++) {         if(x[i] == "a") {             count++;         }     }     return count; } var = "aabbddd"; console.log(counter(a)); 

here go:

function getfrequency(string) {     var freq = {};     (var i=0; i<string.length;i++) {         var character = string.charat(i);         if (freq[character]) {            freq[character]++;         } else {            freq[character] = 1;         }     }      return freq; }; 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -