Home > Software engineering >  Javascript group objects by value
Javascript group objects by value

Time:11-07

Firstly I need to group an array of object with respect to policy attribute's value. Then I need to group action and company fields. I tried to use lodash but could not get to the nested arrays.

From this =>

[
    {
        "action": "VIEW",
        "policy": "FORM"
    },
    {
        "action": "CREATE",
        "policy": "USER"
    },
    {
        "action": "VIEW",
        "policy": "USER"
    },
    {
        "company": "Microsoft",
        "policy": "FORM"
    },
    {
        "company": "Facebook",
        "policy": "USER"
    },
    {
        "company": "Twitter",
        "policy": "USER"
    }
]

to this =>

{
    "FORM": {
            "action": ["VIEW"],
            "company": ["Microsoft"]
        },
    "USER": {
            "action": ["CREATE", "VIEW"],
            "company": ["Facebook", "Twitter"]
        }
}

Thanks a lot

CodePudding user response:

You can do this using a simple for...of loop and some destructuring:

const data = [ { action: 'VIEW', policy: 'FORM', }, { action: 'CREATE', policy: 'USER', }, { action: 'VIEW', policy: 'USER', }, { company: 'Microsoft', policy: 'FORM', }, { company: 'Facebook', policy: 'USER', }, { company: 'Twitter', policy: 'USER', }, ];
let result = {};

for (const { action, policy, company } of data) {
  result[policy] = result[policy] ?? { action: [], company: [] };
  if (action) result[policy].action.push(action);
  if (company) result[policy].company.push(company);
}

console.log(result);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

You could also do this using array#reduce instead of a for loop:

const data = [ { action: 'VIEW', policy: 'FORM' }, { action: 'CREATE', policy: 'USER' }, { action: 'VIEW', policy: 'USER' }, { company: 'Microsoft', policy: 'FORM' }, { company: 'Facebook', policy: 'USER' }, { company: 'Twitter', policy: 'USER' }, ];

const result = data.reduce((result, { action, company, policy }) => {
  result[policy] = result[policy] ?? { action: [], company: [] };
  action && result[policy].action.push(action);
  company && result[policy].company.push(company);
  return result;
}, {});

console.log(result);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

const groupBy = (x, f) => x.reduce((a, b) => ((a[f(b)] ||= []).push(b), a), {})
// by this function you can group by any field value .
// for group by policy 
groupBy(data, v => v.policy)

CodePudding user response:

You could group by policy and separate the rest of the array and build nested groups.

const
    data = [{ action: "VIEW", policy: "FORM" }, { action: "CREATE", policy: "USER" }, { action: "VIEW", policy: "USER" }, { company: "Microsoft", policy: "FORM" }, { company: "Facebook", policy: "USER" }, { company: "Twitter", policy: "USER" }],
    result = Object.values(data.reduce((r, { policy, ...o }) => {
        r[policy] ??= {};
        Object
            .entries(o)
            .forEach(([k, v]) => (r[policy][k] ??= []).push(v));
        return r;
    }, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related