Currently working on my first Node.js API project. Within the '/posts' endpoint, I am receiving data in this format:
[
{
"POST_ID": 1,
"POST_TITLE": "Post N.1",
"POST_DESCRIPTION": "Description for Post N.1",
"POST_PHOTO_URL": "Url for image 1 of post 1"
},
{
"POST_ID": 1,
"POST_TITLE": "Post N.1",
"POST_DESCRIPTION": "Description for Post N.1",
"POST_PHOTO_URL": "Url for image 2 of post 1"
},
{
"POST_ID": 2,
"POST_TITLE": "Post N.2",
"POST_DESCRIPTION": "Description for Post N.2",
"POST_PHOTO_URL": "Url for image 1 of post 2"
},
{
"POST_ID": 2,
"POST_TITLE": "Post N.2",
"POST_DESCRIPTION": "Description for Post N.2",
"POST_PHOTO_URL": "Url for image 2 of post 2"
}
]
I am looking to merge objects with the same POST_ID and create an array of URLs for each post under the key 'postImages'. Here's an example of the desired output:
responseObj = {
postId: 0,
postTitle: "Post N.1",
postDescription: "Description for Post N.1",
postImages: ['first_url', 'second_url'],
};
The SQL Query being used is:
SELECT P.POST_ID, P.POST_TITLE, P.POST_DESCRIPTION, PI.POST_PHOTO_URL FROM POST P INNER JOIN POST_ITEMS AS PI ON P.POST_ID = PI.POST_ID
(SQL SERVER).