仅使用 Lodash,如何隔离数组中出现次数最多的元素?

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

我目前正在学习涵盖 Lodash 的教程,它提示我 return 这个值:

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

给出这个对象数组:

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
    }  
  ]

我尝试了很多方法来解决这个问题,但我似乎无法使用 Lodash 重新创建他们想要的答案。这是我尝试过但无济于事的代码,我能否重现他们的答案:

const mostBlogs = async (blogs) => {
  let result = _.countBy(blogs.map(blog => ({author: blog.author.value})))
  const b = _.maxBy(result, 'blogs');
  console.log(result);

这样的事情能解决您的问题吗?

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

这是一个link to a working example

我觉得你很接近。这样的事情可以工作。切换了 mapcountBy。首先进行计数,然后映射到结果对象

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>