Created
March 9, 2022 00:43
-
-
Save marceloglacial/ee08437a14a1f6bd8718c64d769e4367 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| const boookList = [ | |
| { | |
| id: 1, | |
| category: "action" | |
| }, | |
| { | |
| id: 2, | |
| category: "fiction" | |
| }, | |
| { | |
| id: 3, | |
| category: "drama" | |
| } | |
| ]; | |
| const bookReviews = [ | |
| { | |
| id: 1, | |
| reviews: 130 | |
| }, | |
| { | |
| id: 1, | |
| reviews: 30 | |
| }, | |
| { | |
| id: 2, | |
| reviews: 12 | |
| }, | |
| { | |
| id: 2, | |
| reviews: 2 | |
| }, | |
| { | |
| id: 3, | |
| reviews: 1 | |
| } | |
| ]; | |
| const getCategoryName = (id) => boookList.find((item) => item.id === id); | |
| const categoryReview = bookReviews.map((item) => ({ | |
| ...item, | |
| name: getCategoryName(item.id).category | |
| })); | |
| const mostReviews = categoryReview | |
| .reduce((accumulator, current) => { | |
| const duplicate = accumulator.find((item) => item.id === current.id); | |
| duplicate | |
| ? (duplicate.reviews += current.reviews) | |
| : accumulator.push(current); | |
| return accumulator; | |
| }, []) | |
| .sort((a, b) => b.reviews - a.reviews)[0].name; | |
| console.log(mostReviews); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment