Javascript count duplicates and uniques and add to array

前端 未结 5 1511
无人及你
无人及你 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 10:54

    I would do like this

    var arr = ["a", "a", "b", "c", "c", "a"],
      red = arr.reduce((p,c) => (p[c]++ || (p[c]=1),p),{});
    console.log(red);

提交回复
热议问题