Javascript count duplicates and uniques and add to array

前端 未结 5 1513
无人及你
无人及你 2021-01-26 10:29

I\'m trying to count duplicates in an array of dates and add them to a new array.

But i\'m only getting the duplicates and the amount of times they exist in the array. <

5条回答
  •  执笔经年
    2021-01-26 11:03

    lodash's countBy function will do the trick:

    _.countBy(['a', 'a', 'b', 'c', 'c']) will evaluate to: {a: 2, b: 1, c: 2}

    It does involve adding lodash as a dependency though.

提交回复
热议问题