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

141
Views
Multidemisional array

i am trying to create a multidimensional array from a flat array. but i somehow i can not figure it out.

I manage it to get it 1lvl deep but can not figure out how to go all the way: example :

[
  { id: 'cG9zdDoyODI=', parentId: null, children: [] },
  { id: 'cG9zdDoyODM=', parentId: null, children: [] },
  { id: 'cG9zdDoyODc=', parentId: null, children: [] },
  { id: 'cG9zdDoyODY=', parentId: 'cG9zdDoyODc=', children: [] },
  { id: 'cG9zdDozMDE=', parentId: 'cG9zdDoyODY=', children: [] },
  { id: 'cG9zdDozMDI=', parentId: 'cG9zdDozMDE=', children: [] },
  { id: 'cG9zdDoyODQ=', parentId: 'cG9zdDoyODc=', children: [] },
  { id: 'cG9zdDoyODU=', parentId: 'cG9zdDoyODc=', children: [] } 
]

i am trying to place each object that has a parent into the parents children array.

all the help is welcome, thanks in advance

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

If I understood that good, you wnat to do this:

const data = [
  { id: 'cG9zdDoyODI=', parentId: null, children: [] },
  { id: 'cG9zdDoyODM=', parentId: null, children: [] },
  { id: 'cG9zdDoyODc=', parentId: null, children: [] },
  { id: 'cG9zdDoyODY=', parentId: 'cG9zdDoyODc=', children: [] },
  { id: 'cG9zdDozMDE=', parentId: 'cG9zdDoyODY=', children: [] },
  { id: 'cG9zdDozMDI=', parentId: 'cG9zdDozMDE=', children: [] },
  { id: 'cG9zdDoyODQ=', parentId: 'cG9zdDoyODc=', children: [] },
  { id: 'cG9zdDoyODU=', parentId: 'cG9zdDoyODc=', children: [] } 
];

data.forEach(parent=>data.find(elem=>elem.parentId===parent.id && parent.children.push(elem)));

console.log(data);

about 4 years ago · Santiago Gelvez Report

0

let arr = [
  { id: 'cG9zdDoyODI=', parentId: null, children: [] },
  { id: 'cG9zdDoyODM=', parentId: null, children: [] },
  { id: 'cG9zdDoyODc=', parentId: null, children: [] },
  { id: 'cG9zdDoyODY=', parentId: 'cG9zdDoyODc=', children: [] },
  { id: 'cG9zdDozMDE=', parentId: 'cG9zdDoyODY=', children: [] },
  { id: 'cG9zdDozMDI=', parentId: 'cG9zdDozMDE=', children: [] },
  { id: 'cG9zdDoyODQ=', parentId: 'cG9zdDoyODc=', children: [] },
  { id: 'cG9zdDoyODU=', parentId: 'cG9zdDoyODc=', children: [] } 
];

let parents = arr.filter(elem => elem.parentId === null); // filtered out parents

parents.map(elem => {
    arr.forEach(child => {
        if (elem.id === child.parentId) { // matching children by id
            elem.children.push(child);    // pushing children into parents
        }
    });
});
about 4 years ago · Santiago Gelvez 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!