我想从字符串中获取常用词。
我发现了这个方法: http://jsbin.com/nufimudivu/1/edit
for(var i = 0; i < words.length; i++) {
wordCounts["_" + words[i]] = (wordCounts["_" + words[i]] || 0) + 1;
}
问题是我希望我的输出像
{
{
'word': '_lipsum',
'count': Number here
},
{
<!-- other word here -->
}
}
答案 0 :(得分:2)
您可以简单地将输出转换为您想要的格式:
var wordArr = [];
for (var word in wordCounts) {
wordArr.push({
word: word,
count: wordCounts[word]
})
}
console.log(wordArr)