const arr = ['b', 'c', 'b', 'c', 'a', 'b', 'c']
const obj = {}
arr.forEach(function (item) {
if (obj[item] !== undefined) {
obj[item]++
} else {
obj[item] = 1
}
})
console.log(obj) // ==> {a:1, b: 3, c: 3}
可以计算哪一个元素在数组中存在的次数。
const arr = ['b', 'c', 'b', 'c', 'a', 'b', 'c']
const obj = {}
arr.forEach(function (item) {
if (obj[item] !== undefined) {
obj[item]++
} else {
obj[item] = 1
}
})
console.log(obj) // ==> {a:1, b: 3, c: 3}
可以计算哪一个元素在数组中存在的次数。