Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading