簡體   English   中英

有沒有更好的方法來使用 Lodash 庫重構這個函數?

[英]Is there a better way to refactor this function using the Lodash library?

我的目標是獲取一個博客列表並找到擁有最多博客的作者,該作者返回了一個類似 { author: String, blogs: Number } 的對象。 我成功了,但我想知道使用 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}

這是使用 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM