-3

I have this code:

results = "[ { user_name: 'User 1',
    email: '[email protected]',
    userid: 'HJ10092',
    event_type: 'download',
    country: 'Venezuela',
    doc_type: 'mspowerpoint',
    total: '1' },
  { user_name: 'User 1',
    email: '[email protected]',
    userid: 'HJ10092',
    event_type: 'download',
    country: 'Venezuela',
    doc_type: 'document',
    total: '1' },
  { user_name: 'User 1',
    email: '[email protected]',
    userid: 'HJ10092',
    event_type: 'download',
    country: 'Venezuela',
    doc_type: 'msword',
    total: '2' },
  { user_name: 'User 2',
    email: '[email protected]',
    userid: 'POG0092',
    event_type: 'download',
    country: 'Spain',
    doc_type: 'png',
    total: '3' },
  { user_name: 'User 2',
    email: '[email protected]',
    userid: 'POG0092',
    event_type: 'download',
    country: 'Spain',
    doc_type: 'txt',
    total: '3' }]"


  const groupedDocs = Object.entries(
    results.reduce((acc, { country, email, doc_type, total }) => {
      // Group initialization
      grouping = email[0].v
      if (!acc[grouping]) {
        acc[grouping] = [];
      }
      // Grouping
      // FIX: only pushing the object that contains id and value
      acc[grouping].push({ doc_type, total});
      return acc;
    }, {})

  ).map(([email, count]) => ({ email, count }));
  console.log(".......................>>>>", JSON.stringify(groupedDocs));

It does what I want, but I need to also include the "country" field for each user, and I can't. Something like that:

[
 { "email": "[email protected]",
  "country":"Venezuela",
  "count":[{"doc_type":"mspowerpoint",
  "total":"1"
},
{
  "doc_type":"document",
  "total":"1"
},
{
  "doc_type":"txt",
  "total":"69"
},
{
  "doc_type":"pdf",
  "total":"328"
   }
  ]
 },
 { "email": "[email protected]",
  "country":"Spain",
  "count":[{"doc_type":"mspowerpoint",
  "total":"1"
},
{
  "doc_type":"document",
  "total":"1"
},
{
  "doc_type":"txt",
  "total":"69"
}]}]

I could add the country field to each document type, but I don't want to repeat it so many times. I want it to be an additional key just like "email." Thanks in advance

2
  • Don't forget to tag a language. Looks like javascript? I've added the tag for you. Please correct if needed. Commented May 17 at 17:16
  • 1
    Do you realize results is wrapped as String (wrapped in double-quotes)? In such case it's an invalid JSON anyways. It's not a string? Then... paste that Object literal instead. When asking question you're expected to provide a minimal reproducible example, please edit. Try to keep guess-work out of the equation. Commented May 17 at 18:21

2 Answers 2

0

You could group with the complete data and then get a result for each group.

const
    results = [{ user_name: 'User 1', email: '[email protected]', userid: 'HJ10092', event_type: 'download', country: 'Venezuela', doc_type: 'mspowerpoint', total: '1' }, { user_name: 'User 1', email: '[email protected]', userid: 'HJ10092', event_type: 'download', country: 'Venezuela', doc_type: 'document', total: '1' }, { user_name: 'User 1', email: '[email protected]', userid: 'HJ10092', event_type: 'download', country: 'Venezuela', doc_type: 'msword', total: '2' }, { user_name: 'User 2', email: '[email protected]', userid: 'POG0092', event_type: 'download', country: 'Spain', doc_type: 'png', total: '3' }, { user_name: 'User 2', email: '[email protected]', userid: 'POG0092', event_type: 'download', country: 'Spain', doc_type: 'txt', total: '3' }],
    groupedDocs = Object
        .values(Object.groupBy(results, ({ email }) => email))
        .map(group => ({
            email: group[0].email,
            country: group[0].country,
            count: group.map(({ doc_type, total }) => ({ doc_type, total }))
        }));
  
console.log(groupedDocs);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

0

Try this using the reduce and map functions:

const groupedDocs = Object.entries(
  results.reduce((acc, { country, email, doc_type, total }) => {
    const key = `${email}|${country}`;
    if (!acc[key]) {
      acc[key] = { country, email, count: [] };
    }
    acc[key].count.push({ doc_type, total });
    return acc;
  }, {})
).map(([key, value]) => value);

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.