//利用遍历
const arr = ["b", "c","b", "c","a", "b", "c"]
const obj = {}
arr.forEach(item=>{
if(obj[item]){
obj[item]++
}else{
obj[item] = 1
}
})
console.log(obj)
{b: 3, c: 3, a: 1}
//利用reduce
const arr = ["b", "c", "b", "c", "a", "b", "c"];
const obj = arr.reduce((obj, key) => {
if (key in obj) {
obj[key]++;
} else {
obj[key] = 1;
}
return obj;
}, {});
console.log(obj);