📌  相关文章
📜  计算数组中每个元素的数量 javascript 代码示例

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

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

for (var i = 0; i < arr.length; i++) {
  var num = arr[i];
  counts[num] ? counts[num] + 1 : 1;
}

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