I have an array of objects. I want to group the objects by a value and then use that value as a key for the rest of the values in that object.
For example I have array like this:
{
  0: {prize: "Foo", first_name: "John", last_name: "Smith"},
  1: {prize: "Foo", first_name: "Mary", last_name: "Smith"},
  2: {prize: "Bar", first_name: "Jane", last_name: "Doe"},
  3: {prize: "Bar", first_name: "Jack", last_name: "Jones"},
  4: {prize: "Foo", first_name: "Judy", last_name: "Alvarez"},
}
And my desired outcome is something like this:
{
  Foo: [
   {first_name: "John", last_name: "Smith"},
   {first_name: "Mary", last_name: "Smith"},
   {first_name: "Judy", last_name: "Alvarez"}
  ],
  Bar: [
   {first_name: "Jane", last_name: "Doe"}, 
   {first_name: "Jack", last_name: "Jones"}
  ]
}
I'm using TypeScript and the closest I got was using this code snippet I found:
console.log(
  _.chain(res.data)
  .groupBy("prize")
  .map((value: any, key: any) => ({prize: key, winners: value}))
  .value()
);
Which gave me an outcome like this:
[
  0: {
      prize: "Foo", 
      winners: [
       {prize: "Foo", first_name: "John", last_name: "Smith"}, 
       {prize: "Foo", first_name: "Mary", last_name: "Smith"},
       {prize: "Foo", first_name: "Judy", last_name: "Alvarez"}
      ]
     },
  1: {
      prize: "Bar", 
      winners: [
       {prize: "Bar", first_name: "Jane", last_name: "Doe"}, 
       {prize: "Bar", first_name: "Jack", last_name: "Jones"}
      ]
     },
]
How would I modify my code to achieve the format I need effectively? I'm not even completely sure I'm on the right track, maybe the code needs to be completely changed?
This seems like a quite common problem and has probably been answered before but because I have a hard time even describing what I want, I couldn't find anything. I'm sorry if this is a duplicate.
 
     
    