简体   繁体   中英

Get total number of likes for each user in an array of objects - JavaScript

So I'm stuck with a logical problem. I'm trying to figure out a way of getting total number of likes for each user in an array of objects. Here is my Sample array.

const articles = [{
  authorId: "2vP3i2rYvDYPrikE1k1DTaZDeoq1",
  dislikes: 0,
  likes: 3,
  neutral: 0,
  text: "This is Mike Alice's first Shot!!",
  username: "mikealice",
  id: "NMFGbeTvSYddsr7VzjFV",
},
 {
  authorId: "2vP3i2rYvDYPrikE1k1DTaZDeoq1",
  dislikes: 0,
  likes: 2,
  neutral: 0,
  text: "This is Mike Alice's second Shot!!",
  username: "mikealice",
  id: "NMFGbeTvSYddsr7VzjFV",
},
 {
  authorId: "2vP3i2rYvDYPrikE1k1klkDTaZDeoq1",
  dislikes: 0,
  likes: 1,
  neutral: 0,
  text: "This is admin's first Shot!!",
  username: "admin",
  id: "NMFGbeTvSYddsr7VzjFV",
},
 {
  authorId: "2vP3i2rYvDYPrikE1k1klkDTaZDeoq1",
  dislikes: 0,
  likes: 3,
  neutral: 0,
  text: "This is admin's second Shot!!",
  username: "admin",
  id: "NMFGbeTvSYddsr7VzjFV",
}]

My goal is to have a final list of of distinct username with their total likes like the one bellow.

const userLikes = [
  {
  username: 'mikealice',
  totalLikes: 5
  },
  {
  username: 'admin',
  totalLikes: 4
  }
]

Help me figure out the logic to solving this problem. Thanks.

  1. Take an empty object, say obj
  2. Loop through your array
  3. On every loop, check for if obj has already a key same as the current username
  4. If not then, create a key value pair, key would be the username , value would be another object containing username and totalLikes
  5. If there is already a key same as username , increase the value of totalLikes with current likes
  6. After the loop is finished, you will have your answer, but in a dictionary / object form
  7. Object.values will bring out the desired array

Code -

 let obj = {} articles.forEach((article, index) => { let { username, likes } = article if (!obj[username]) { obj[username] = { username, totalLikes: likes } } else { let { totalLikes } = obj[username] obj[username].totalLikes = totalLikes + likes } }) let finalArray = Object.values(obj)

Using reduce and Object.values will simplify.

 const getTotalLikes = arr => Object.values( arr.reduce((acc, { username, likes }) => { acc[username] = username in acc ? { username, totalLikes: acc[username].totalLikes + likes } : { username, totalLikes: likes }; return acc; }, {}) ); const articles = [ { authorId: "2vP3i2rYvDYPrikE1k1DTaZDeoq1", dislikes: 0, likes: 3, neutral: 0, text: "This is Mike Alice's first Shot!!", username: "mikealice", id: "NMFGbeTvSYddsr7VzjFV" }, { authorId: "2vP3i2rYvDYPrikE1k1DTaZDeoq1", dislikes: 0, likes: 2, neutral: 0, text: "This is Mike Alice's second Shot!!", username: "mikealice", id: "NMFGbeTvSYddsr7VzjFV" }, { authorId: "2vP3i2rYvDYPrikE1k1klkDTaZDeoq1", dislikes: 0, likes: 1, neutral: 0, text: "This is admin's first Shot!!", username: "admin", id: "NMFGbeTvSYddsr7VzjFV" }, { authorId: "2vP3i2rYvDYPrikE1k1klkDTaZDeoq1", dislikes: 0, likes: 3, neutral: 0, text: "This is admin's second Shot!!", username: "admin", id: "NMFGbeTvSYddsr7VzjFV" } ]; console.log(getTotalLikes(articles));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM