I have a situation where my SQL queries are returning multiple objects due to a many-to-many mapping in express. I am in search of a tool that can help me simplify these common objects by nesting arrays of objects within them.
SELECT *
FROM User
LEFT JOIN UserAddress ON UserAddress.user_id = User.user_id
LEFT JOIN Address ON Address.address_id = UserAddress.address_id;
When we execute this query, the response looks something like:
[
{
"user_id": 1,
"name": "test name",
"address_id": 1,
"address": "1234 address way"
},
{
"user_id": 1,
"name": "test name",
"address_id": 2,
"address": "5678 new address"
},
{
"user_id": 2,
"name": "diff name",
"address_id": 1,
"address": "1234 address way"
}
]
What I am aiming for is to transform this array using a JavaScript tool so it resembles the following structure:
[
{
"user_id": 1,
"name": "test name",
"address": [
{
"address_id": 1,
"address": "1234 address way"
},
{
"address_id": 2,
"address": "5678 new address"
}
]
},
{
"user_id": 2,
"name": "diff name",
"address_id": 1,
"address": "1234 address way"
}
]
However, I am uncertain about what this transformation process is formally called or if there exists a specific tool that can facilitate this operation?