misbha afreen
misbha afreen

Reputation: 33

I wanted a help in creating a javascript function to group JSON elements

I have JSON data in this format as mentioned in policyTree as shown below

    let policyTree = [
        {"serviceType": "pcf-sm","policyProject": "Policy_001","policy":"Nchf_001"},
        {"serviceType": "pcf-sm", "policyProject": "Policy_001","policy":"PRA_001"},
        {"serviceType": "pcf-sm", "policyProject" : "Policy_002","policy":"main"},
        {"serviceType": "pcf-ue", "policyProject": "Policy_003", "policy":"ET_001"},
        {"serviceType": "pcf-ue", "policyProject" : "abc", "policy": "main"}
      ]

I need to convert the data as shown in the below block. I'm trying to acheive the following output as shown below. Looking out for a javascript function which would do the work.

    [
      {
        "serviceType": "pcf-sm",
        "policyProject": [
          {
            "name": "Policy_001",
            "policies": [
              "main",
              "Nchf_001",
              "PRA_001",
            ]
          },
          {
            "name": "Policy_002",
            "policies": [
              "main"
            ]
          }
        ]
      },
      {
        "serviceType": "pcf-ue",
        "policyProject": [
          {
            "name": "Policy_003",
            "policies": [
              "ET_001"
            ]
          },
          {
            "name": "abc",
            "policies": [
              "main"
            ]
          }
        ]
      }
    ]

I have tried the following to achieve its structure,I have tried grouping serviceType, but i also have to group policyProject with its policies

let policyTree = [
            {"serviceType": "pcf-sm","policyProject": "Policy_001","policy":"Nchf_001"},
            {"serviceType": "pcf-sm", "policyProject": "Policy_001","policy":"PRA_001"},
            {"serviceType": "pcf-sm", "policyProject" : "Policy_001","policy":"main"},
            {"serviceType": "pcf-ue", "policyProject": "Policy_002", "policy":"ET_001"},
            {"serviceType": "pcf-ue", "policyProject" : "abc", "policy": "main"}
          ]

          console.log(
            Object.values(policyTree.reduce((a, { serviceType, policyProject, policy }) => {
              if (!a[serviceType]) a[serviceType] = { serviceType, policyProject: [], };
              a[serviceType].policyProject.push({"name": policyProject, "policies": [policy]}); 
              return a;
            }, {}))
          );

Upvotes: 0

Views: 82

Answers (1)

Josh Stevens
Josh Stevens

Reputation: 4221

i have written you a script to do this think it matches what you want, hope it helps!

let policyTree = [
  { serviceType: 'pcf-sm', policyProject: 'Policy_001', policy: 'Nchf_001' },
  { serviceType: 'pcf-sm', policyProject: 'Policy_001', policy: 'PRA_001' },
  { serviceType: 'pcf-sm', policyProject: 'Policy_002', policy: 'main' },
  { serviceType: 'pcf-ue', policyProject: 'Policy_003', policy: 'ET_001' },
  { serviceType: 'pcf-ue', policyProject: 'abc', policy: 'main' },
];

let result = policyTree.reduce((results, input) => {
  const existingServiceType = results.find(
    (c) => c.serviceType === input.serviceType
  );
  if (existingServiceType) {
    const existingPolicyProjectIndex = existingServiceType.policyProject.findIndex(
      (p) => p.name === input.policyProject
    );
    if (existingPolicyProjectIndex !== -1) {
      existingServiceType.policyProject[
        existingPolicyProjectIndex
      ].policies.push(input.policy);
    } else {
      existingServiceType.policyProject.push({
        name: input.policyProject,
        policies: [input.policy],
      });
    }
  } else {
    results.push({
      serviceType: input.serviceType,
      policyProject: [
        {
          name: input.policyProject,
          policies: [input.policy],
        },
      ],
    });
  }

  return results;
}, []);

console.log(result);

Upvotes: 1

Related Questions