我正在使用 Handlebars 在网页上显示数组。数组应该填充数据,然后我想从中获取最常用的单词并将结果显示在网页上。
// Display the array on index page
app.get('/', function(req, res) {
res.render('index', {title: 'my webpage', data: result});
});
空数组:
var theArray = [];
将内容推送到数组theArray
:
function getData(err, data, response) {
var content = data.statuses;
for (var i = 0; i < content.length; i++) {
theArray.push( content[i].text );
}
}
从 中获取最常用的词theArray
:
if (theArray === undefined || theArray.length == 0) {
console.log('array is empty');
}
distribution = {},
max = 0,
result = [];
theTweets.forEach(function (a) {
distribution[a] = (distribution[a] || 0) + 1;
if (distribution[a] > max) {
max = distribution[a];
result = [a];
return;
}
if (distribution[a] === max) {
result.push(a);
}
});
result.push(result);
当我设置data: theArray
并向其推送内容时,一切正常并显示数组的内容。但是当我改为data: result
显示最常用的单词时,我只会得到“数组为空”。
我究竟做错了什么?