使用foreach遍历文章中出现所有单词的次数

var tweets = [
		    "Education is showing business the way by using technology to share information. How do we do so safely?",
		    "Enjoy a free muffin & coffee with Post Plus, our new loyalty club exclusive to subscribers!",
		    "We're LIVE on Periscope right now answering all your #pet questions - tweet us yours now!",
		    "Saw an unfinished print of M:I5 yesterday and it is my great pleasure to report it is rather splendid.",
		    "Downloading the beta of OS X El Capitan. This is going to gooch my mac for sure, but it will probably look pretty",
		    "Mud pies and muscle: H.G. Wells weighs in on #STEMeducation in an automated age",
		    "UK Space Agency brings astronaut’s mission to children across the UK #ESA #ESERO",
		    "Trying to find an old tweet that I didn't favourite at the time is…a fools game. #continuesDigging",
		    "A robot has just passed a classic test of self-awareness. What does that mean?",
		    "It was a great event. Very important to learn how to communicate about science and engineering effectively"
		];
		
		var words = {};
		//tweetText将数组tweets中所有的元素转化成以空格为分隔符的字符串
		var tweetText = tweets.join(" ");
		//tweetWords将字符串tweetText转化成以空格为分隔符的数组
		var tweetWords = tweetText.split(" ");
		
		//初始化让所有数组对应的次数为0
		//word是tweetWords数组中的每一项的value值,用数组的形式表现出来
		tweetWords.forEach(function (word) {
		    words[word] = 0;
		});
		
		//只要出现一次就加一
		
		tweetWords.forEach(function (word) {
		    words[word] = words[word] + 1;
		});
		
		console.log(words);

以此可以遍历出各个字母出现的次数:


var tweets = [
  "Education is showing business the way by using technology to share information. How do we so so safely?",
  "Enjoy a free muffin & coffee with Post Plus, our new loyalty club exclusive to subscribers!",
  "We're LIVE on Periscope right now answering all your #pet questions - tweet us yours now!",
  "Saw an unfinished print of M:I5 yesterday and it is my great pleasure to report it is rather splendid.",
  "Downloading the beta of OS X El Capitan. This is going to gooch my mac for sure, but it will probably look pretty",
  "Mud pies and muscle: H.G. Wells weighs in on #STEMeducation in an automated age",
  "UK Space Agency brings astronaut’s mission to children across the UK #ESA #ESERO",
  "Trying to find an old tweet that I didn't favourite at the time is…a fools game. #continuesDigging",
  "A robot has just passed a classic test of self-awareness. What does that mean?",
  "It was a great event. Very important to learn how to communicate about science and engineering effectively"
];

var letters = {};
var tweetText = tweets.join("");
var tweetLetters = tweetText.split("");
  
tweetLetters.forEach(function (letter) {
    letters[letter.toLowerCase()] = 0;
});

tweetLetters.forEach(function (letter) {
    letters[letter.toLowerCase()] += 1;
});

console.log(letters);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值