擅长:python、mysql、java
<p>您可以稍微修改它以接受来自文本文件的输入</p>
<pre><code>var letterArray = "asdf\njkasdf\nthe\nsadf".split('\n');
function count(letterArray) {
let mapping = {};
for (let i=0; i < letterArray.length; i++){
if (mapping[letterArray[i]] !== undefined){ // if the letter already exists in the mapping increment it
mapping[letterArray[i]] += 1;
}else { //if the letter does not exist add it and initialize it
mapping[letterArray[i]] = 1;
}
}
return mapping;
}
console.log("count: ", count(letterArray));
</code></pre>