I'm trying to get the most popular/common word from an Array, I've tried the following but instead of it just saying 'Rain' it displays this inside the console instead [rain: 2, hot: 1].
What am I doing wrong? I only want to display the number one most popular words without the number.
Any help/advice would be helpful, thank you.
var defaultArray = [{age:"25-35",country:"united kingdom",sex:"male",word:"rain"},{age:"25-35",country:"united arab emirates",sex:"male",word:"hot"},{age:"25-35",country:"zimbabwe",sex:"female",word:"rain"}];
/* Popular */
function popular() {
var words = [];
for(var p = 0; p < defaultArray.length; p++){
var word = defaultArray[p].word;
words.push(word);
console.log(words);
}
var count = [];
for (var i = 0, j = words.length; i < j; i++) {
if (count[words[i]]) {
count[words[i]]++;
}
else {
count[words[i]] = 1;
}
}
console.log(count);
}
popular();