Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
JS: Arrange object data based on occurrences

I need to somehow arrange the data below to a certain format like below:

  const data: {} = [
    { emotion: 3, intensity: 1 },
    { emotion: 5, intensity: 2 },
    { emotion: 3, intensity: 1 },
    { emotion: 3, intensity: 2 },
  ]

  const dataone = _.groupBy(data, 'emotion')

  const entries: any = Object.entries(dataone)
  console.log(entries)

To this format:

 [{emotion: 3, 1: 2, 2: 1, total: 3},
 {emotion: 5, 1: 0, 2: 1, total: 1}]

Would appreciate it if you could provide any help or link to some similar questions.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could group and update the values.

const
    data = [{ emotion: 3, intensity: 1 }, { emotion: 5, intensity: 2 }, { emotion: 3, intensity: 1 }, { emotion: 3, intensity: 2 }],
    result = Object.values(data.reduce((r, { emotion, intensity }) => {
        r[emotion] ??= { 1: 0, 2: 0, emotion, total: 0 };
        r[emotion][intensity]++;
        r[emotion].total++;
        return r;
    }, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!