JavaScript中数组中出现的单词

JavaScript中数组中出现的单词,javascript,Javascript,我有一个数组 array=["hello","goodbye","hello","hello","goodbye"]; 我希望这个数组计算每个单词的出现次数,打印一个包含每个单词的表,以及在JavaScript中数组被看到的次数。你能帮我吗?试试这个: var array=["hello","goodbye","hello","hello","goodbye"]; var count = {}; for (var i = 0; i < array.length; i++) { c

我有一个数组

array=["hello","goodbye","hello","hello","goodbye"];
我希望这个数组计算每个单词的出现次数,打印一个包含每个单词的表,以及在JavaScript中数组被看到的次数。你能帮我吗?

试试这个:

var array=["hello","goodbye","hello","hello","goodbye"];
var count = {};
for (var i = 0; i < array.length; i++) {
    count[array[i]] = (count[array[i]] || 0) + 1;
}
console.log(count);
var数组=[“你好”、“再见”、“你好”、“你好”、“再见”];
变量计数={};
对于(var i=0;i
HTML输出:

var table = document.createElement('table');
for (var word in count) {
    var tr = document.createElement('tr');
    tr.innerHTML = '<td>' + word + '</td><td>' + count[word] + '</td>';
    table.appendChild(tr);
}
document.body.appendChild(table);
var table=document.createElement('table');
for(计数中的var字){
var tr=document.createElement('tr');
tr.innerHTML=''+word+''+count[word]+'';
表1.儿童(tr);
}
document.body.appendChild(表);

+1但为什么要字符串化?是的,没有理由。刚刚删除了它。当我想打印表中每一行的元素时,我怎么做?@user3238361你的意思是想在HTML表中输出它,对吗?我正在用JavaScript创建表。