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

182
Views
Javascript map over nested children

I would like to extract only certain properties from a nested object. For example, I would like a new array that does not include the 'percentDone', 'rollup' & 'color'. Using the map function, I'm not sure how to access all the nested children.

const tasks = [
  {
    id: 1000,
    name: 'Launch SaaS Product',
    percentDone: 50,
    startDate: '2019-01-14',
    expanded: true,
    children: [
      {
        id: 1,
        name: 'Setup web server',
        percentDone: 50,
        duration: 10,
        startDate: '2019-01-14',
        rollup: true,
        endDate: '2019-01-23',
        expanded: true,
        children: [
          {
            id: 11,
            name: 'Install Apache',
            percentDone: 50,
            startDate: '2019-01-14',
            rollup: true,
            duration: 3,
            color: 'teal',
            endDate: '2019-01-17',
            cost: 200,
            baselines: [
              {
                startDate: '2019-01-13T23:00:00',
                endDate: '2019-01-16T23:00:00'
              },
              {
                startDate: '2019-01-13T23:00:00',
                endDate: '2019-01-16T23:00:00'
              },
              {
                startDate: '2019-01-13T23:00:00',
                endDate: '2019-01-16T23:00:00'
              }
            ]
          }
        ],
        baselines: [
          {
            startDate: '2019-01-13T23:00:00',
            endDate: '2019-01-22T23:00:00'
          },
          {
            startDate: '2019-01-13T23:00:00',
            endDate: '2019-01-22T23:00:00'
          },
          {
            startDate: '2019-01-13T23:00:00',
            endDate: '2019-01-22T23:00:00'
          }
        ]
      }
    ],
    endDate: '2019-03-16',
    baselines: [
      {
        startDate: '2019-01-13T23:00:00',
        endDate: '2019-03-15T23:00:00'
      },
      {
        startDate: '2019-01-13T23:00:00',
        endDate: '2019-03-15T23:00:00'
      },
      {
        startDate: '2019-01-13T23:00:00',
        endDate: '2019-03-15T23:00:00'
      }
    ]
  }
];

Something like this but this not include all the nested children.

 var x = tasks.map(item => {
      return {
        name: item.name,
        StartDate: item.StartDate,
        etc... 
        children: item.children.map(x => ({ 
          name: x.name,
          StartDate: x.StartDate,
    
        }))
      }  
    });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I would extract it to a different function but you could also inline it like this:

    var x = tasks.map(function mapper(item) {
      return {
        name: item.name,
        StartDate: item.StartDate,
        // etc... 
        children: item.children.map(mapper),
      }  
    });
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!