Skip to content

Instantly share code, notes, and snippets.

@marceloglacial
Created March 9, 2022 00:43
Show Gist options
  • Select an option

  • Save marceloglacial/ee08437a14a1f6bd8718c64d769e4367 to your computer and use it in GitHub Desktop.

Select an option

Save marceloglacial/ee08437a14a1f6bd8718c64d769e4367 to your computer and use it in GitHub Desktop.
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