【发布时间】:2018-07-18 02:23:12
【问题描述】:
我正在尝试做词频,并且我有一些工作可以打印出词频。我现在想按升序对输出进行排序。
var paragraph = "Into the bored patent composes the synonymous cheer. The playing essence mothers an offset. Does the alleged cap fast? Why can't the covered fish urge the word? The cyclist works within a laughing jam. When will my smooth year entitle the public?";
un_puncutated_paragraph = paragraph.replace(/[~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g,"");
let wordsHolder = un_puncutated_paragraph.split(' ');
let frequencyMap = {};
wordsHolder.forEach((word) => {
if (!frequencyMap[word]) {
frequencyMap[word] = 0;
}
frequencyMap[word] += 1;
});
另外,有人可以更好地解释这个逻辑,不完全确定发生了什么吗?
if (!frequencyMap[word]) {
frequencyMap[word] = 0;
}
frequencyMap[word] += 1;
谢谢!
【问题讨论】:
-
所以.. 你写了这段代码但不明白你写了什么?或者你从某个地方复制了这段代码并想理解它?数组上有一个
sort方法... -
所以如果我没记错的话,你想按长度升序显示更新的段落文本吗?
-
我写了除了 if 块之外的所有东西。我正在尝试按升序对频率图的输出进行排序。因此,例如 Into 被计算了 6 次,因为它应该在打印 frequencyMap 时位于顶部。
-
请查看我的回答给我您的反馈。
标签: javascript algorithm sorting