每次遇到数组中的特定值时,使用reduce()来递增计数器。

 1 const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0); 2 3 // countOccurrences([1,1,2,1,2,3], 1) -> 3 

相关文章:

  • 2021-08-15
  • 2021-07-22
  • 2021-11-20
  • 2022-12-23
  • 2021-10-27
  • 2022-12-23
  • 2021-06-13
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2023-01-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案