第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么這個函數(shù)返回{作者:Robert C. Martin,喜歡:NaN},當(dāng)數(shù)組較大時

為什么這個函數(shù)返回{作者:Robert C. Martin,喜歡:NaN},當(dāng)數(shù)組較大時

守著星空守著你 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])}當(dāng)單個博客大小 === 1 時,工作不工作,但當(dāng)輸入 = >  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,    },    ]不知道該怎么辦,嘗試過實現(xiàn)不同的方法,但現(xiàn)在有點碰壁了。想知道是否有更好的方法?
查看完整描述

3 回答

?
慕姐4208626

TA貢獻1852條經(jīng)驗 獲得超7個贊

問題在于夏季減速器。它需要一個包含屬性的對象。但是,它返回一個數(shù)字。第一次調(diào)用 summer 時,它接收具有屬性的對象,但是,第二次調(diào)用它時,它會收到一個數(shù)字(不包含 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]);

};


查看完整回答
反對 回復(fù) 2022-08-04
?
DIEA

TA貢獻1820條經(jīng)驗 獲得超3個贊

看起來你有未聲明的變量:author

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


查看完整回答
反對 回復(fù) 2022-08-04
?
qq_笑_17

TA貢獻1818條經(jīng)驗 獲得超7個贊

下面是類型對象,代碼嘗試用對象添加一個數(shù)字。而不是聲明為對象,僅以 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)


查看完整回答
反對 回復(fù) 2022-08-04
  • 3 回答
  • 0 關(guān)注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號