Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

229
Vistas
Using only Lodash, how do i isolate an element that occurs the most in an array?

I am currently following a tutorial that covers Lodash and it is prompting me to return this value :

{
  author: "Robert C. Martin",
  blogs: 3
}

given this array of objects:

const blogs = [
    {
      _id: "5a422a851b54a676234d17f7",
      title: "React patterns",
      author: "Michael Chan",
      url: "https://reactpatterns.com/",
      likes: 7,
      __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: 5,
      __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
    }  
  ]

I have tried a copious amount of ways to figure this out but i cannot seem to recreate the answer they want using only Lodash. Here is the code I tried but, to no avail, was I able to reproduce their answer:

const mostBlogs = async (blogs) => {
  let result = _.countBy(blogs.map(blog => ({author: blog.author.value})))
  const b = _.maxBy(result, 'blogs');
  console.log(result);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I think you are pretty close. Something like this could work. Switched the map and countBy. First did the counting and then mapped over the result object

const blogs = [{_id: "5a422a851b54a676234d17f7",title: "React patterns",author: "Michael Chan",url: "https://reactpatterns.com/",likes: 7,__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: 5,__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}]
  

let result =_.map(_.countBy(blogs,'author'),(val,key)=>({author:key,blogs:val}))
const maxRes = _.maxBy(result, 'blogs');
console.log(maxRes);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Does something like this solve your problem?

const author = "Robert C. Martin"
console.log({
  author: author,
  blogs: _.filter(blogs, (blog) => blog.author === author).length
})

Here's a link to a working example.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda