Last active
June 9, 2021 01:55
-
-
Save stoneboyindc/1b298074eb23b13c8eea431699a9b72f to your computer and use it in GitHub Desktop.
getMostCommonGenres()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getMostCommonGenres(books) { | |
let countObj = {}; | |
books.forEach(aBook => { | |
if (countObj[aBook.genre] != null) { | |
countObj[aBook.genre]++; | |
} else { | |
countObj[aBook.genre] = 1; | |
} | |
}); | |
let countArray = []; | |
for (const [key, value] of Object.entries(countObj)) { | |
countArray.push({ | |
'name' : key, | |
'count' : value | |
}); | |
} | |
countArray.sort((a,b) => b.count - a.count); | |
return countArray.slice(0, 5); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getMostCommonGenres(books) { | |
let result = {}; | |
let genre = books.forEach((book) => { | |
if (result[book.genre] == null) { | |
result[book.genre] = 1; | |
} else { | |
result[book.genre] += 1; | |
} | |
}) | |
let countArray = []; | |
for (const [key, value] of Object.entries(result)) { | |
countArray.push({ | |
'name' : key, | |
'count' : value | |
}); | |
} | |
countArray.sort((a,b) => b.count - a.count); | |
return countArray.slice(0, 5); | |
} |
Author
stoneboyindc
commented
May 10, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment