是否有更好的方法可以使用Lodash库来重构此功能?

发布于 2025-01-28 20:47:33 字数 992 浏览 3 评论 0原文

我的目标是列出博客列表,并找到最多的博客返回这样的对象的作者{作者:string,博客:numbers}。我很成功,但我想知道使用lodash的解决方案的更好方法。谢谢你!

let blogs = [
    { title: 'sorceror stone', author: 'rowling', likes: 5 },
    { title: 'prisoner of azkaban', author: 'rowling', likes: 13 },
    { title: 'green mile', author: 'king', likes: 3 },
    { title: 'oliver twist', author: 'dickens', likes: 7 },
    { title: 'the half blood prince', author: 'rowling', likes: 16 },
    { title: 'david copperfield', author: 'dickens', likes: 10 },
    { title: 'christmas carol', author: 'dickens', likes: 3 },
    { title: 'tale of two cities', author: 'dickens', likes: 20 },
]

const mostBlogs = (blogs) => {
    let authorsByBlogs = _.countBy(blogs, 'author')

    let maxBlogs = _.max(_.values(authorsByBlogs))

    let author = _.findKey(authorsByBlogs, (o) => {
        return o === maxBlogs
    })

    return { author: author, blogs: maxBlogs }
}

console.log(mostBlogs(blogs)) // { author: "dickens", blogs: 4}

My goal was to take a list of blogs and find the author with the most blogs returning an object like this { author: String, blogs: Number }. I was successful, but I want to know a better approach to my solution using Lodash. Thank You!

let blogs = [
    { title: 'sorceror stone', author: 'rowling', likes: 5 },
    { title: 'prisoner of azkaban', author: 'rowling', likes: 13 },
    { title: 'green mile', author: 'king', likes: 3 },
    { title: 'oliver twist', author: 'dickens', likes: 7 },
    { title: 'the half blood prince', author: 'rowling', likes: 16 },
    { title: 'david copperfield', author: 'dickens', likes: 10 },
    { title: 'christmas carol', author: 'dickens', likes: 3 },
    { title: 'tale of two cities', author: 'dickens', likes: 20 },
]

const mostBlogs = (blogs) => {
    let authorsByBlogs = _.countBy(blogs, 'author')

    let maxBlogs = _.max(_.values(authorsByBlogs))

    let author = _.findKey(authorsByBlogs, (o) => {
        return o === maxBlogs
    })

    return { author: author, blogs: maxBlogs }
}

console.log(mostBlogs(blogs)) // { author: "dickens", blogs: 4}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南冥有猫 2025-02-04 20:47:33

这是使用Lodash的_。Flow()(请参阅代码中的注释)的重构功能的重构。

const { flow, countBy, toPairs, maxBy, last, zipObject } = _

const mostBlogs = flow(
  blogs => countBy(blogs, 'author'), // count by the author
  toPairs, // convert to array of [key, value] pairs
  blogs => maxBy(blogs, last), // get the entry with most blogs
  blog => zipObject(['author', 'blogs'], blog) // convert to an object
)

const blogs = [{"title":"sorceror stone","author":"rowling","likes":5},{"title":"prisoner of azkaban","author":"rowling","likes":13},{"title":"green mile","author":"king","likes":3},{"title":"oliver twist","author":"dickens","likes":7},{"title":"the half blood prince","author":"rowling","likes":16},{"title":"david copperfield","author":"dickens","likes":10},{"title":"christmas carol","author":"dickens","likes":3},{"title":"tale of two cities","author":"dickens","likes":20}]

const result = mostBlogs(blogs)

console.log(result) // { author: "dickens", blogs: 4}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

并且使用 lodash/fp

const { flow, countBy, toPairs, maxBy, last, zipObject } = _

const mostBlogs = flow(
  countBy('author'), // count by the author
  toPairs, // convert to array of [key, value] pairs
  maxBy(last), // get the entry with most blogs
  zipObject(['author', 'blogs']) // convert to an object
)

const blogs = [{"title":"sorceror stone","author":"rowling","likes":5},{"title":"prisoner of azkaban","author":"rowling","likes":13},{"title":"green mile","author":"king","likes":3},{"title":"oliver twist","author":"dickens","likes":7},{"title":"the half blood prince","author":"rowling","likes":16},{"title":"david copperfield","author":"dickens","likes":10},{"title":"christmas carol","author":"dickens","likes":3},{"title":"tale of two cities","author":"dickens","likes":20}]

const result = mostBlogs(blogs)

console.log(result) // { author: "dickens", blogs: 4}
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

This is a refactor of your function using lodash's _.flow() (see comments in code):

const { flow, countBy, toPairs, maxBy, last, zipObject } = _

const mostBlogs = flow(
  blogs => countBy(blogs, 'author'), // count by the author
  toPairs, // convert to array of [key, value] pairs
  blogs => maxBy(blogs, last), // get the entry with most blogs
  blog => zipObject(['author', 'blogs'], blog) // convert to an object
)

const blogs = [{"title":"sorceror stone","author":"rowling","likes":5},{"title":"prisoner of azkaban","author":"rowling","likes":13},{"title":"green mile","author":"king","likes":3},{"title":"oliver twist","author":"dickens","likes":7},{"title":"the half blood prince","author":"rowling","likes":16},{"title":"david copperfield","author":"dickens","likes":10},{"title":"christmas carol","author":"dickens","likes":3},{"title":"tale of two cities","author":"dickens","likes":20}]

const result = mostBlogs(blogs)

console.log(result) // { author: "dickens", blogs: 4}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

And the same idea using lodash/fp:

const { flow, countBy, toPairs, maxBy, last, zipObject } = _

const mostBlogs = flow(
  countBy('author'), // count by the author
  toPairs, // convert to array of [key, value] pairs
  maxBy(last), // get the entry with most blogs
  zipObject(['author', 'blogs']) // convert to an object
)

const blogs = [{"title":"sorceror stone","author":"rowling","likes":5},{"title":"prisoner of azkaban","author":"rowling","likes":13},{"title":"green mile","author":"king","likes":3},{"title":"oliver twist","author":"dickens","likes":7},{"title":"the half blood prince","author":"rowling","likes":16},{"title":"david copperfield","author":"dickens","likes":10},{"title":"christmas carol","author":"dickens","likes":3},{"title":"tale of two cities","author":"dickens","likes":20}]

const result = mostBlogs(blogs)

console.log(result) // { author: "dickens", blogs: 4}
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文