我想计算文本中的字母并获取对象以显示每个单词的字母计数。计算文本中的字母并生成带结果的对象
因此,对于输入"Argument here"
预期输出是:
{ Argument: { A: 1, r: 2, g: 1, u: 1, m: 1, e: 3, n: 1, t: 1, ' ': 1, h: 1 },
here: { e: 2, h: 1, r: 1} }
我尝试:
function LetterCountI(str) {
var arr = str.split(" "); var cCount = {}; var anotherO = {}; var final = {};
for(var i = 0; i < arr.length; i++){
if(cCount[arr[i]]){
cCount[arr[i]]++;
} else {
cCount[arr[i]] = 1;
}
}
var letterCount = str.split("");
for(var j = 0; j < letterCount.length; j++){
if(anotherO[letterCount[j]]){
anotherO[letterCount[j]]++;
} else {
anotherO[letterCount[j]] = 1;
}
}
for(var property in cCount){
final[property] = anotherO;
}
return final;
}
LetterCountI("Argument here");
我目前得到的是
{ Argument: { A: 1, r: 2, g: 1, u: 1, m: 1, e: 3, n: 1, t: 1, ' ': 1, h: 1 },
here: { A: 1, r: 2, g: 1, u: 1, m: 1, e: 3, n: 1, t: 1, ' ': 1, h: 1 } }
'让数= S => [] reduce.call(S,(A,C)=>(一[c] =(a [c] || 0)+ 1,a),{})',然后为每个单词分割并计数。 – elclanrs
无法理解问题。你能解释一下什么是对象和你期望的输出? – vinit
据我所知:你有一个字符串,你需要产生一个对象,其中包含文本的文字作为属性,每个属性包含一个对象,其中包含字母作为属性和单词中字母的数量作为值 – smnbbrv