group array and get count [duplicate]

Just use the function Array.prototype.reduce.

let array = [   "foo",   "bar",   "foo",   "bar",   "bar",   "bar",   "zoom"],
    result = array.reduce((a, c) => (a[c] = (a[c] || 0) + 1, a), Object.create(null));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Leave a Comment