我想要計算唯一字符在字符串中出現多少次。 但我被困在對象迭代,其中它只返回1無論性格多少次同樣出現多個時間:JavaScript對象迭代與for循環
function allUniqueCha(word) {
var wordObj = {};
var wordArray = word.split([, ]);
wordArray.forEach(function(c) {
wordObj[c] = +1;
});
console.log(wordArray);
console.log(wordObj);
}
allUniqueCha("Bubble")
輸出爲:{B:1, U:1,b:1,1:1,E:1},但關鍵的 「b」 的預期值應該爲2
什麼是'wordObj [C]'的初始值?當你給它分配'+ 1'會發生什麼? – RobG