Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to format json object by specific value in NODEjs

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": "m.m@gmx.de"
      },
      {
        "groupname": "TestGroup001",
        "description": "Test Gruppe für xyz...",
        "mgID": 1,
        "uID": 98,
        "username": "Susanne S.",
        "email": "s.s@gmx.de"
      },
      {
        "groupname": "TestGroup002",
        "description": "Test Gruppe für abc...",
        "mgID": 22,
        "uID": 96,
        "username": "Moritz A.",
        "email": "m.a@gmx.de"
      },
      {
        "groupname": "TestGroup002",
        "description": "Test Gruppe für abc...",
        "mgID": 22,
        "uID": 95,
        "username": "Lennart H",
        "email": "l.h@gmx.de"
      }
    ]
  }

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": "m.m@gmx.de"
          },
          {
            "uID": 98,
            "username": "Susanne S.",
            "email": "s.s@gmx.de"
          }
        ]
      },
      {
        "groupname": "TestGroup002",
        "description": "Test Gruppe für abc...",
        "mgID": 22,
        "users": [
          {
            "uID": 96,
            "username": "Moritz A.",
            "email": "m.a@gmx.de"
          },
          {
            "uID": 95,
            "username": "Lennart H",
            "email": "l.h@gmx.de"
          }
        ]
      }
    ]
  }

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i dont need working code…just some keywords.

>Solution :

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: "m.m@gmx.de",
    },
    {
      groupname: "TestGroup001",
      description: "Test Gruppe für xyz...",
      mgID: 1,
      uID: 98,
      username: "Susanne S.",
      email: "s.s@gmx.de",
    },
    {
      groupname: "TestGroup002",
      description: "Test Gruppe für abc...",
      mgID: 22,
      uID: 96,
      username: "Moritz A.",
      email: "m.a@gmx.de",
    },
    {
      groupname: "TestGroup002",
      description: "Test Gruppe für abc...",
      mgID: 22,
      uID: 95,
      username: "Lennart H",
      email: "l.h@gmx.de",
    },
  ],
};

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading