0

In my NodeJS method i make a simple select that return a response like:

    [{id: 1, project: "project A", user: "Tom", duration: 8},
{id: 2, project: "project B", user: "Mark", duration: 8},
{id: 1, project: "project A", user: "Mark", duration: 8},
{id: 3, project: "project C", user: "Tom", duration: 8},
{id: 3, project: "project C", user: "Mark", duration: 8},
{id: 3, project: "project C", user: "Helen", duration: 8}]

How can i achieve to edit this array so that the final response that is sent in frontend to look like this:

    [{project: {projectName: "project A", users: [{userName: "Tom", duration: 8},
                                                  {userName: "Mark", duration: 8}]},
    {projectName: "project B", users: [{userName: "Mark", duration: 8}]},
    {projectName: "project C", users: [{userName: "Tom", duration: 8},
                                       {userName: "Mark", duration: 8},
                                       {userName: "Helen", duration: 8}]}

Somehow I need to make an array of objects with the users and durations that are on the same project.

If it is more helpful, the general idea is that I have to create a monthly report that will get every user with worked hours on every project.

If someone can help me, thank you!

  • 1. the response that you provided is not a valid JSON. 2. in the response there's only a single "project" which make it unclear. 3. what have you tried? – Nir Alfasi Nov 25 '20 at 13:01
  • why it's not a valid JSON? if it's because of the id, my sql is grouped by project name and user name. There are 3 projects in the json with 3 people that worked randomly or not at all on them. I tried to reduce the array of objects but it didn't really work well yet. –  Nov 25 '20 at 13:08
  • 1
    Why don't you do it on the sql side using GROUP BY projectName? – kg99 Nov 25 '20 at 13:33
  • my sql have GROUP BY projectName and userName but i have more users that worked on the same project. As a general idea, i have to create reports at the end of the month to bring every user with it's hours worked on every project –  Nov 25 '20 at 13:47
  • i found something that helped me in FE here: https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key thank you for your interest guys! –  Nov 25 '20 at 13:58

0 Answers0