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

202
Views
How can I group an array?

I have this array :

array=[{x:2,y:test0,z:title},{x:3,y:test0,z:title},{x:4,y:test0,z:title},{x:5,y:test0,z:title2},{x:6,y:test0,z:title2},{x:7,y:test0,z:title2}]

And I turned it into this array this way:

 const groups = array.reduce((acc, row) => {
       acc[row[title]] = acc[row[title]] || [];
        acc[row[title]].push(row);
        return acc;
      }, {});

this is result:

  [
    title:[{x:2,y:test0},{x:3,y:test0},{x:4,y:test0}]
    title2:[{x:5,y:test1},{x:6,y:test1},{x:7,y:test1}]
    ]

And I want to have this array:

[
 title:[test0:[{x:2},{x3:},{x:4}]]
 title2:[test1:[{x:5}],test2:[{x:6}],test3:[{x:7}]]]
]

How can I get to this array?

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

0

You want y to be the key. You can just use object destructuring like:

const {y, ...obj} = row[title];
const fixedRow = {[y]: obj};

In this way y gets removed from the object, and you can use the previous value of y as key and the remaining properties as value.

This won't look exactly like your expected result, because arrays can't have properties, but I suppose it's just a typo.

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!