我之前見過類似的問題,例如計算給定字串中的字符數。但是,當涉及將給定字串與字母表中的字母進行比較并回傳出現以下情況的物件時:
const letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
const sampleString = "a bee";
const results = {
a: 1,
b: 1,
c: 0,
d: 0,
e: 2,
f: 0,
...
}
uj5u.com熱心網友回復:
我們可以使用Array.reduce()來計算 sampleString 中的字母。
我們首先創建 aletterMap來指定要計算的所有有效字母。
在reduce 回圈中,我們只使用(c in acc) 運算式遞增出現在letterMap 中的字母。
const letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
const sampleString = "a bee";
const letterMap = letters.reduce((acc, c) => {
acc[c] = 0;
return acc;
}, {});
const result = [...sampleString].reduce((acc, c) => {
if (c in acc) acc[c] ;
return acc;
}, letterMap);
console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
這是另一種方法,只使用一個回圈,再次使用Array.reduce(),這假設我們不希望計算空格:
const letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
const sampleString = "a bee";
const result = [...letters, ...sampleString].reduce((acc, c) => {
if (c in acc) {
acc[c] ;
} else if (c.trim()) {
acc[c] = 0;
}
return acc;
}, {});
console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
uj5u.com熱心網友回復:
使用Array.reduce和String.match可能是一個想法。因此,對于 的每個字母letters,使用match(length) 來確定該字母在給定樣本中的頻率。
const letters = `abcdefghijklmnopqrstuvwxyz`.split(``);
const freq = (chr, sample) => (sample.match(RegExp(chr, `g`)) || []).length;
const result = letters.reduce( (acc, chr) =>
({...acc, [chr]: freq(chr, acc.sample)}), {sample: "a bee"});
console.log(result);
uj5u.com熱心網友回復:
或者更難以理解的方式
const letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
const sampleString = "a bee";
results = {};
sampleString.forEach(letter=>{
if(letters.includes(letter)) {
if(results[letter] === undefined) results[letter]=0;
results[letter] ;
}
});
uj5u.com熱心網友回復:
我喜歡用Object.fromEntries這個:
const sampleString = "a bee";
const result = Object.fromEntries(Array.from("abcdefghijklmnopqrstuvwxyz", ch => [ch, 0]));
for (let ch of sampleString) result[ch] ;
console.log(result);
uj5u.com熱心網友回復:
將值作為鍵是一種不好的做法,因為它很難閱讀,我建議將其命名為:
const letters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
const tempString = "This is a string.";
let results = []
letters.forEach( char =>{
const count = countCharacterOccurences(tempString, char)
results.push({ alphabet: char, count })
})
function countCharacterOccurences(string, char) {
return string.split(char).length - 1;
}
console.log(results)
//filter alphabet with value
const alphabetWithCount = results.filter( result => {
return result.count > 0
})
console.log(alphabetWithCount)
//log alphabet only with counts
const alphabetOnlyWithCounts = alphabetWithCounts.map( alphabetWithCount => {
return alphabetWithCount.alphabet
})
console.log(alphabetOnlyWithCounts)
//
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/376385.html
標籤:javascript 数组
上一篇:只向DOM添加新的陣列元素
