javascript - 当数组较大时,为什么此函数返回 {author : Robert C. Martin, likes: NaN}

标签 javascript arrays

const mostLikes = (blogs) => {
  if (!blogs.length) {
    return 0
  }

  const distinctAuthors = [...new Set(blogs.map((blog) => blog.author))]
  const summer = (prev, comp) => prev.likes + comp.likes

  console.log(distinctAuthors)
  const dummyAuth = {
    author: 'hmm',
    likes: 0,
  }

  const authorsWithLikes = distinctAuthors.map((author) => ({
    author,
    likes: blogs.filter((n) => n.author === author).reduce(summer, dummyAuth),
  }))

  const reducer = (prev, comp) => (prev[1] > comp[1] ? prev : comp)
  return authorsWithLikes.reduce(reducer, authorsWithLikes[0])
}

当单个博客大小 === 1 时正常工作,但当输入 => 时则不工作

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

不知道该怎么做,尝试过实现不同的方法,但现在遇到了障碍。不知道是否还有更好的方法?

最佳答案

问题出在夏季 reducer 上。它需要一个包含 likes 属性的对象。但是,它返回一个数字。第一次调用 Summer 时,它会收到具有 likes 属性的 dummyAuth 对象,但是,第二次调用时,它会收到一个数字(不包含 Like 属性) )。

您可以通过让 Summer 返回一个具有 likes 属性的对象来解决该问题。

const mostLikes = (blogs) => {
  if (!blogs.length) {
    return 0;
  }

  const distinctAuthors = [...new Set(blogs.map((blog) => blog.author))];
  const summer = (prev, comp) => ({ likes: prev.likes + comp.likes });

  const dummyAuth = {
    author: 'hmm',
    likes: 0,
  }

  const authorsWithLikes = distinctAuthors.map((author) => ({
    author,
    likes: blogs.filter((n) => n.author === author).reduce(summer, dummyAuth ).likes, // note: you have to access the `likes` property 
  }));

  const reducer = (prev, comp) => (prev[1] > comp[1] ? prev : comp);
  return authorsWithLikes.reduce(reducer, authorsWithLikes[0]);
};

关于javascript - 当数组较大时,为什么此函数返回 {author : Robert C. Martin, likes: NaN},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61663761/

相关文章:

c - 将数组传递给 C 中的线程例程

javascript - TaffyDB - 将数据呈现为 HTML

javascript - 无法将光标放在移动设备上充满空白的 TEXTAREA 上

java - 插入排序 - 计算反转次数

java - 需要帮助将 TAB 写入 toString 方法

javascript - 如何显示和映射状态或 const 数据数组对象的第一个 id?

javascript - 使用单引号内的转义引号解析 JSON 时出错

JavaScript jQuery : call function with parameters after timeout?

javascript - Next.JS:如何处理 getInitialProps 中的错误?

c - 多维数组随机悬挂