I have come across a data structure related question that I believe would be best addressed in this forum. Recently, I have been encountering the following issue frequently. I receive data from a service in the following format. It consists of an array of people and their corresponding pets.
owners = [
{
owner: 'anne',
pets: ['ant', 'bat']
},
{
owner: 'bill',
pets: ['bat', 'cat']
},
{
owner: 'cody',
pets: ['cat', 'ant']
}
];
However, I am interested in having an array of pets and the owners who possess them, as shown below:
pets = [
{
pet: 'ant',
owners: ['anne', 'cody']
},
{
pet: 'bat',
owners: ['anne', 'bill']
},
{
pet: 'cat',
owners: ['bill', 'cody']
}
];
Is there a tool available that can automatically transform my input array into an array of unique pet objects, where each object includes a property with an array of owners?
Alternatively, do I have to manually code this transformation?