📌  相关文章
📜  javascript 从列表中获取每个元素计数的出现频率 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:37.159000             🧑  作者: Mango

代码示例1
const arr = [5, 5, 5, 2, 2, 2, 2, 2, 9, 4];
const counts = {};

for (const num of arr) {
  counts[num] = counts[num] ? counts[num] + 1 : 1;
}

console.log(counts[5], counts[2], counts[9], counts[4]);
 Run code snippet