我有一个数组["academy"]
,我需要计算数组中字符串的字符数。
输出:
a:2
c:1
d:1
e:1
m:1
y:1
像这样
我尝试了两个for循环
function sumChar(arr){
let alph="abcdefghijklmnopqrstuvxyz";
let count=0;
for (const iterator of arr) {
for(let i=0; i<alph.length; i++){
if(iterator.charAt(i)==alph[i]){
count++;
console.log(`${iterator[i]} : ${count}`);
count=0;
}
}
}
}
console.log(sumChar(["abdulloh"]));
它工作不正常
a : 1
b : 1
h : 1
undefined