group similar key-value pair to a single array

I have a array similar to this one:

[
   {
      "username":"player1",
      "nickname":"PLayer1",
      "capturedate":"06/12/2022",
      "capturetime":"10:05PM"
   },
   {
      "username":"player1",
      "nickname":"PLayer1",
      "capturedate":"06/12/2022",
      "capturetime":"10:00PM"
   },
   {
      "username":"player1",
      "nickname":"PLayer1",
      "capturedate":"06/12/2022",
      "capturetime":"10:10PM"
   },
   {
      "username":"player1",
      "nickname":"PLayer1",
      "capturedate":"06/12/2022",
      "capturetime":"10:15PM"
   },
   {
      "username":"player2",
      "nickname":"player2",
      "capturedate":"06/12/2022",
      "capturetime":"10:00PM"
   },
   {
      "username":"player2",
      "nickname":"player2",
      "capturedate":"06/12/2022",
      "capturetime":"10:05PM"
   }
]

I want to transform it so that it looks like this. Basically all of the capturedate and capturetime would be merged to a single array of just entries.

[
   {
      "username":"player1",
      "nickname":"Player1",
      "entries":[
         {
            "capturedate":"06/12/2022",
            "capturetime":"10:05PM"
         },
         {
            "capturedate":"06/12/2022",
            "capturetime":"10:10PM"
         },
         {
            "capturedate":"06/12/2022",
            "capturetime":"10:10PM"
         },
         {
            "capturedate":"06/12/2022",
            "capturetime":"10:15PM"
         }
      ]
   },
   {
      "username":"player2",
      "nickname":"Player2",
      "entries":[
         {
            "capturedate":"06/12/2022",
            "capturetime":"10:00PM"
         },
         {
            "capturedate":"06/12/2022",
            "capturetime":"10:05PM"
         }
      ]
   }
]

I tried implementing the one from here Most efficient method to groupby on an array of objects but it’s giving a different set.

>Solution :

What you are probably missing is the Object.values step. since the post you mentioned has answers which does the grouping only giving the final output an object. To get the values of the grouped object you will need to use Object.values

let arr = [   {      "username":"player1",      "nickname":"PLayer1",      "capturedate":"06/12/2022",      "capturetime":"10:05PM"   },   {      "username":"player1",     "nickname":"PLayer1",      "capturedate":"06/12/2022",      "capturetime":"10:00PM"   },   {      "username":"player1",      "nickname":"PLayer1",      "capturedate":"06/12/2022",      "capturetime":"10:10PM"   },   {      "username":"player1",      "nickname":"PLayer1",      "capturedate":"06/12/2022",      "capturetime":"10:15PM"   },   {      "username":"player2",      "nickname":"player2",      "capturedate":"06/12/2022",      "capturetime":"10:00PM"   },   {      "username":"player2",      "nickname":"player2",      "capturedate":"06/12/2022",      "capturetime":"10:05PM"   }]

let res = Object.values(arr.reduce((acc,{username,nickname,...rest})=>{
    acc[username] = acc[username] || {username,nickname,entries:[]}
  acc[username].entries.push(rest)
  return acc
},{}))

console.log(res)

Leave a Reply