So I have this data, which contains an array of objects, the objects are blogs info, and they have authors, title, likes, link, etc.
What I wan't to do is get the author with the most likes across this array of data.
const blogs = [ { _id: "5a422a851b54a676234d17f7", title: "React patterns", author: "Michael Chan", url: "https://reactpatterns.com/", __v: 0 }, { _id: "5a422aa71b54a676234d17f8", title: "Go To Statement Considered Harmful", author: "Edsger W. Dijkstra", url: "http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html", likes: null, __v: 0 }, { _id: "5a422b3a1b54a676234d17f9", title: "Canonical string reduction", author: "Edsger W. Dijkstra", url: "http://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD808.html", likes: 12, __v: 0 }, { _id: "5a422b891b54a676234d17fa", title: "First class tests", author: "Robert C. Martin", url: "http://blog.cleancoder.com/uncle-bob/2017/05/05/TestDefinitions.htmll", likes: 10, __v: 0 }, { _id: "5a422ba71b54a676234d17fb", title: "TDD harms architecture", author: "Robert C. Martin", url: "http://blog.cleancoder.com/uncle-bob/2017/03/03/TDD-Harms-Architecture.html", likes: 0, __v: 0 }, { _id: "5a422bc61b54a676234d17fc", title: "Type wars", author: "Robert C. Martin", url: "http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html", likes: 2, __v: 0 }
for example:
getAuthorWithMostLikes(blogs)
And it would check the array, sum the number of likes for every author across every article and return for example:
{author: Robert C. Martin, totalLikes: 12}
How can I achieve this in a clean way? I already "solved" it but I think the code is pretty messy and not readable
Here's my code:
const likesByAuthor = (blogs) =>{
const blogsWhereThereIsAnAuthor = blogs.filter(blog=> blog.author)
console.log("length of filtered array", blogsWhereThereIsAnAuthor.length)
let authorsAndLikes = []
let contin = undefined;
blogsWhereThereIsAnAuthor.forEach((blog, index) => {
let author = blog.author;
let likes = blog.likes;
// initialize concentrado
console.log("loop for: ", author)
if (!likes){
return false
}
if (authorsAndLikes.length < 1){
authorsAndLikes.push({author, likes})
console.log("initial data", authorsAndLikes)
return
}
authorsAndLikes.forEach((item) => {
console.log(item.author, "|", blog.author, item.author === blog.author)
if (item.author === blog.author) {
console.log("case 1: existing author", item.author, "|", blog.author)
console.log(item)
item.likes += blog.likes;
console.log(item)
console.log(authorsAndLikes)
contin = false;
return false;
}
contin = true;
})
if (contin !== false) {
console.log("case 2: new author, adding it to concentrado array", blog.author)
authorsAndLikes.push({"author": author, "likes": likes})
console.log(authorsAndLikes)
console.log("end of iteration case 2")
}
})
return authorsAndLikes;
}
const mostLikes = (blogs) =>{
const likesCountByAuthor = likesByAuthor(blogs)
return likesCountByAuthor.reduce((curr, sum) => curr.likes > sum.likes ? curr : sum)
}
How can I achieve the same result with more clean code or in a more efficient way?