'Is there an efficient way to create a map from an array which stores count of keys in Typescript?

Suppose I have an array a, a = ['John', 'John', 'Bill', 'John', 'Bill ', 'Sally'], is there an easy way to produce a map, m, that produces the following using Typescript, m = {{'John', 3}, {'Bill', 2}, {'Sally', 1}} ?

I've been stuck on producing an efficient way without the use of a loop. Python for example has the Counter function which one can use to quickly produce a dictionary with every key in a list along with its count.

Does anyone happen to know if Typescript has a built-in function that solves this problem, or will I have to ultimately use a loop?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source