标签: javascript arrays object
let arr = [1, 2, 3, 2, 3], obj = {}; for (let val of arr) { obj[val] = (obj[val] || 0) + 1; } console.log(obj);
输出应为:
obj = { 1: 1, 2: 2, 3: 3 }