michaelpri michaelpri - 1 year ago 85
Javascript Question

Is there a Javascript function similar to the Python Counter function?

I am attempting to change a program of mine from Python to Javascript and I was wondering if there was a JS function like the Counter function from the collections module in Python.

Syntax for Counter

from collection import Counter
list = ['a', 'b', 'c', 'b', 'a', 'b', 'c', 'a', 'a', 'a']
counter = Counter(list)
print counter


Counter({'a':5, 'b':3, 'c':2})

Answer Source

You can use Lo-Dash's countBy function:

var list = ['a', 'b', 'c', 'b', 'a', 'b', 'c', 'a', 'a', 'a'];

JSFiddle example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download