0

I have an array that looks like this one (it has more objects, but the structure is the same):

[
  {
      especiality: "surgery",
      users: [
          {
              id: "182",
              country: "Colombia",
              province: "Bogota",
              telephone: "211112212",
              neighbourhood: "La Santa"
              region: "South",
          },
          {
            id: "182",
            country: "Venezuela",
            province: "Caracas",
            telephone: "322323333",
            region: "North",
        },
        {
          id: "183",
          country: "Brasil",
          telephone: "23232333",
          neighbourhood: "Santos"
          region: "South",
      },
      ]
  },

The user is determined by the ID and the address is determined by four properties. I currently have this function which returns an address that joins all of the users by their IDs:

getGroups = test => {
  return test.map(item => {
     item.groupedUsers = [_.groupBy(item.users, user => [user.id])]
     return item
  })
}

The thing is, I also want the addresses, if the ID is the same, to compose one single array (I need to map these elements). The outlook should look like this one:

user: [{id: 182, locations[(if they exist)               
              country: "Colombia",
              province: "Bogota",
              telephone: "211112212",
              neighbourhood: "La Santa"
              region: "South"]}]

Any ideas on how I could go about this? Since I need to account for values that MAY NOT exist, it´s not the same as the duplicated question.

0 Answers0