πŸš€ DevOps Certified Professional
πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 | 🀝 +1 (469) 756-6329 πŸ” Contact@DevOpsSchool.com

How to count which word occurrence is highest in objects of an array

DevOps

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Requirement


Which word occurs the most frequently in the check one field, such as answer one, answer two, or answer three, per the specified criteria

const wordCounts = {
answerone: 0,
answertwo: 0,
answerthree: 0,
answerfour: 0
};
data[0].checkone.forEach(item => {
if (item === 'answerone') {
wordCounts.answerone++;
} else if (item === 'answertwo') {
wordCounts.answertwo++;
} else if (item === 'answerthree') {
wordCounts.answerthree++;
} else if (item === 'answerfour') {
wordCounts.answerfour++;
}
});
let highestWord = '';
let highestCount = 0;
for (const word in wordCounts) {
if (wordCounts[word] > highestCount) {
highestWord = word;
highestCount = wordCounts[word];
}
}
console.log('Highest word:', highestWord);
console.log('Highest count:', highestCount);
view raw gistfile1.txt hosted with ❀ by GitHub
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x