function count(str) {
const obj = {}
// 去除空格
const arr = str.replace(/\s*/g, "").split('')
arr.forEach(item => {
if (!obj[item]) {
obj[item] = 1
} else {
obj[item] += 1
}
});
return obj
} 涉及到的:
1.需要去除掉字符串的空格,使用到数组的replace方法和正则表达式,字符串转数组的split(' ')
2.方括号语法

京公网安备 11010502036488号