为了账号安全,请及时绑定邮箱和手机立即绑定

为什么这个函数返回{作者:Robert C. Martin,喜欢:NaN},当数组较大时

为什么这个函数返回{作者:Robert C. Martin,喜欢:NaN},当数组较大时

守着星空守着你 2022-08-04 16:47:25
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,    },    ]不知道该怎么办,尝试过实现不同的方法,但现在有点碰壁了。想知道是否有更好的方法?
查看完整描述

3 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

问题在于夏季减速器。它需要一个包含属性的对象。但是,它返回一个数字。第一次调用 summer 时,它接收具有属性的对象,但是,第二次调用它时,它会收到一个数字(不包含 like 的属性)。likesdummyAuthlikes


您可以通过使 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]);

};


查看完整回答
反对 回复 2022-08-04
?
DIEA

TA贡献1820条经验 获得超2个赞

看起来你有未声明的变量:author

blogs.filter((n) => n.author === author)


查看完整回答
反对 回复 2022-08-04
?
qq_笑_17

TA贡献1818条经验 获得超7个赞

下面是类型对象,代码尝试用对象添加一个数字。而不是声明为对象,仅以 0 初始化dummyAuthdummyAuthlikes


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,

}, ]


const mostLikes = (blogs) => {

  if (!blogs.length) {

    return 0

  }


  const distinctAuthors = [...new Set(blogs.map((blog) => blog.author))];

  const summer = (prev, comp) => prev + comp.likes;

  const likes = 0;


  const authorsWithLikes = distinctAuthors.map((author) => {

    return {

      author,

      likes: blogs.filter((n) => n.author === author).reduce(summer, likes)

    };

  });

  console.log(authorsWithLikes);

  const reducer = (prev, comp) => (prev[1] > comp[1] ? prev : comp);

  return authorsWithLikes.reduce(reducer, authorsWithLikes[0])

};

mostLikes(blogs)


查看完整回答
反对 回复 2022-08-04
  • 3 回答
  • 0 关注
  • 122 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信