0

I have a json object in NODE like this:

{
    "data": [
      {
        "groupname": "TestGroup001",
        "description": "Test Gruppe für xyz...",
        "mgID": 1,
        "uID": 1,
        "username": "Max M.",
        "email": "[email protected]"
      },
      {
        "groupname": "TestGroup001",
        "description": "Test Gruppe für xyz...",
        "mgID": 1,
        "uID": 98,
        "username": "Susanne S.",
        "email": "[email protected]"
      },
      {
        "groupname": "TestGroup002",
        "description": "Test Gruppe für abc...",
        "mgID": 22,
        "uID": 96,
        "username": "Moritz A.",
        "email": "[email protected]"
      },
      {
        "groupname": "TestGroup002",
        "description": "Test Gruppe für abc...",
        "mgID": 22,
        "uID": 95,
        "username": "Lennart H",
        "email": "[email protected]"
      }
    ]
  }

and i need it grouped by "groupname" or better by "mgID" like that..

{
    "data": [
      {
        "groupname": "TestGroup001",
        "description": "Test Gruppe für xyz...",
        "mgID": 1,
        "users": [
          {
            "uID": 1,
            "username": "Max M.",
            "email": "[email protected]"
          },
          {
            "uID": 98,
            "username": "Susanne S.",
            "email": "[email protected]"
          }
        ]
      },
      {
        "groupname": "TestGroup002",
        "description": "Test Gruppe für abc...",
        "mgID": 22,
        "users": [
          {
            "uID": 96,
            "username": "Moritz A.",
            "email": "[email protected]"
          },
          {
            "uID": 95,
            "username": "Lennart H",
            "email": "[email protected]"
          }
        ]
      }
    ]
  }

i tried it with object.map...without success also with object.entries...also without success

i dont need working code...just some keywords.

2

1 Answer 1

0

A code like this will do that:

const result = {
  data: Object.values(
    input.data.reduce((groups, current) => {
      const { mgID, description, groupname, ...rest } = current;
      if (!(mgID in groups)) {
        groups[mgID] = {
          mgID,
          description,
          groupname,
          users: [],
        };
      }
      groups[mgID].users.push(rest);
      return groups;
    }, {})
  ),
};

const input = {
  data: [
    {
      groupname: "TestGroup001",
      description: "Test Gruppe für xyz...",
      mgID: 1,
      uID: 1,
      username: "Max M.",
      email: "[email protected]",
    },
    {
      groupname: "TestGroup001",
      description: "Test Gruppe für xyz...",
      mgID: 1,
      uID: 98,
      username: "Susanne S.",
      email: "[email protected]",
    },
    {
      groupname: "TestGroup002",
      description: "Test Gruppe für abc...",
      mgID: 22,
      uID: 96,
      username: "Moritz A.",
      email: "[email protected]",
    },
    {
      groupname: "TestGroup002",
      description: "Test Gruppe für abc...",
      mgID: 22,
      uID: 95,
      username: "Lennart H",
      email: "[email protected]",
    },
  ],
};

const result = {
  data: Object.values(
    input.data.reduce((groups, current) => {
      const { mgID, description, groupname, ...rest } = current;
      if (!(mgID in groups)) {
        groups[mgID] = {
          mgID,
          description,
          groupname,
          users: [],
        };
      }
      groups[mgID].users.push(rest);
      return groups;
    }, {})
  ),
};
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.