无法更新用于计数字母的对象属性编号(计数器)



我是JavaScript的新手,我正在尝试计算bigLettersArr中每个字母表的数量。我做了一个for循环来遍历bigLettersArr中的每个元素并更新计数器,但不知何故它不起作用。同样,我所要做的就是更新alphabet对象计数器以获得每个字母的数量。

也,我认为可能有更聪明的方法来做到这一点,我查找了数组的其他方法,但无法找到并提出更好的代码。

这是我的代码。感谢您的阅读!

const bigLettersArr = [ "X","E","Q","M","L","L","L","B","X","C","A","F",
"X","W","B","W","A","F","D","Z","B","C","L","F",
"M","E","B","E","A","M","L","L","B","G","L","G",
"H","F","H","G","M","A","F","B","B","C","G","F",
"H","Z","B","L","L","H","J","L"]
//made an empty array with all alphabet A-Z 
const alphabet = {
'A': 0, 'B': 0, 'C': 0, 'D': 0, ... 'Z':0
}
//Count how many each alphabet in the array.   
for(let letter in bigLettersArr){
let targetLetter = bigLettersArr[letter]
// console.log(targetLetter)
alphabet.targetLetter += 1
}

console.log(alphabet)

数组在JS中是可迭代的。所以你可以在这里使用for..ofloop

你应该使用for..in循环与objectsfor..ofarrays

……in语句遍历对象中所有以字符串为关键字的可枚举属性(忽略以symbol为关键字的),包括继承的可枚举属性。- mdn

……语句创建一个循环迭代可迭代对象,包括:内置String, Array,类数组对象(例如,参数或NodeList), TypedArray, Map, Set和用户定义的可迭代对象。它调用一个自定义迭代钩子,其中包含针对对象的每个不同属性值执行的语句。- mdn

您也可以使用forEach代替for..of作为:

bigLettersArr.forEach(letter => alphabet[letter] += 1);

const bigLettersArr = ["X", "E", "Q", "M", "L", "L", "L", "B", "X", "C", "A", "F",
"X", "W", "B", "W", "A", "F", "D", "Z", "B", "C", "L", "F",
"M", "E", "B", "E", "A", "M", "L", "L", "B", "G", "L", "G",
"H", "F", "H", "G", "M", "A", "F", "B", "B", "C", "G", "F",
"H", "Z", "B", "L", "L", "H", "J", "L"]
const alphabet = Array
.from( { length: 26 }, ( _, i ) => String.fromCharCode( i + 65 ))
.reduce( ( acc, curr ) => ( ( acc[curr] = 0 ), acc ), {} );
for ( let letter of bigLettersArr ) {
alphabet[letter] += 1
}
console.log( alphabet )
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

您正在尝试增加targetLetter属性。

用括号代替:

for (let letter in bigLettersArr) {
let targetLetter = bigLettersArr[letter]
alphabet[targetLetter] += 1
}

my way…

const bigLettersArr = 
[ 'X', 'E', 'Q', 'M', 'L', 'L', 'L', 'B', 'X', 'C'
, 'A', 'F', 'X', 'W', 'B', 'W', 'A', 'F', 'D', 'Z'
, 'B', 'C', 'L', 'F', 'M', 'E', 'B', 'E', 'A', 'M'
, 'L', 'L', 'B', 'G', 'L', 'G', 'H', 'F', 'H', 'G'
, 'M', 'A', 'F', 'B', 'B', 'C', 'G', 'F', 'H', 'Z'
, 'B', 'L', 'L', 'H', 'J', 'L'
] 
const alphabet = Object.fromEntries(Array.from({length:26},(_,i)=>[String.fromCharCode('A'.charCodeAt(0)+i),0]))
bigLettersArr.forEach( letter => { alphabet[letter]++ })
console.log( alphabet )
.as-console-wrapper { max-height: 100% !important; top: 0 }

如果不存在的字符不重要,可以直接使用reduce

const alphabetWithCounts = bigLettersArr.reduce((acc, cur) => {
const count = acc[cur] ? acc[cur] + 1 : 1;
return {...acc, [cur]: count }
}, {})

更简洁的代码

const alphabetWithCounts = bigLettersArr.reduce((acc, cur) => ({...acc, [cur]: acc[cur] ? acc[cur] + 1 : 1 }), {})

错误如下:

  • 您正在使用for…在循环中,这种循环遍历对象的可枚举,如果要遍历可迭代对象的(如数组、集合或映射),则必须使用for…of。对于数组,您将获得数组所具有的索引。
  • 点表示法访问具有字面名称的字段,如果你想访问具有动态名称的值,你应该使用括号表示法(obj[name])。

下面的演示也使用了truthiness:

const bigLettersArr = []; // fill it with some values later
const alphabet = {}; //  initialize alphabet
//Count how many each alphabet in the array.
for (const letter of bigLettersArr) {
if (alphabet[letter]) {
// if the alphabet already has that letter we increment the count
alphabet[letter] += 1;
} else {
// or else we initialize it
alphabet[letter] = 1;
}
}
console.log(alphabet);

相关内容

  • 没有找到相关文章

最新更新