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

325
Views
How can I create an array of objects by extracting an array that is inside another array of objects in Javascript/Typescript?

I am trying to create a new array based on another array of objects that has another array of objects within its keys. To explain myself. I have the following interfaces:

export interface  RouteElement{
  component?:  Type<any>;
  path: string;
  label: string;
  data?:{ message?: string}

}

export interface  RouterNodeElement{
  label: string;
  children: RouteElement[]
}

and I have this array

routerNodes: [ { label: "NodeRoute", children: [ { path: 'child', label: 'child',  data: { message: 'child' }}] } ]

So basically I have an array of routerNodes with the key inside "Children" and what I want is to extract all the "Children" and create a new array with each children of each routerNodel but I can't find a way to flatten that object.

const flatRouterNodes = this.routerNodes.map( route => {
    return route.children.map( child => child);
  });

What I got

enter image description here

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

0

Is this what you're looking for?

const routeNodes = [ { label: "NodeRoute", children: [ { path: 'child', label: 'child',  data: { message: 'child' }}] }, { label: "R2", children: [ { path: 'child', label: 'child',  data: { message: 'child' }}] } ]
routeNodes.reduce((acc, curr) => [...acc, ...curr.children], [])
// [
//  { path: 'child', label: 'child', data: { message: 'child' } },
//  { path: 'child', label: 'child', data: { message: 'child' } }
// ]

You can also use flatMap:

routerNodes.flatMap((item) => item.children)

Note that flatMap doesn't work in IE 11, but seems to be more performant than reduce (see benchmark).

about 4 years ago · Juan Pablo Isaza Report

0

You can use reduce and forEach

let routerNodes = [ { label: "NodeRoute", children: [ { path: 'child', label: 'child',  data: { message: 'child' }}] } ]

let childerns = routerNodes.reduce((acc,curr)=>{
  curr.children.forEach(child => acc.push(child))
  return acc
},[])

console.log(childerns)


let routerNodes = [{label: "NodeRoute",children: [{path: 'child',label: 'child',data: {message: 'child'}}]},{label: "NodeRoute2",children: [{path: 'child2',label: 'child2',data: {message: 'child2'}}]}]

let childerns = routerNodes.flatMap(curr => curr.children)

console.log(childerns)

You can use flatMap as well

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!