group array and get count

前端 未结 6 471
执念已碎
执念已碎 2021-01-12 07:00

Say I have array like this:

[
   \"foo\",
   \"bar\",
   \"foo\"
   \"bar\",
   \"bar\",
   \"bar\",
   \"zoom\"
]

I want to group it so I

6条回答
  •  时光取名叫无心
    2021-01-12 07:55

    Yeah I guess with Array.prototype.reduce, it's just:

      const map = list.reduce((a, b) => {
                a[b] = a[b] || 0;
                return ++a[b], a;
              }, {});
    

    wondering if there is less verbose way tho.

提交回复
热议问题