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

201
Views
How do i perform an operation on a child element and return the parent?

I have an array like this

const array = [
  { 
    name: 'Parent Brand 1', 
    childBrands: [
      { name: 'Child Brand 1', status: 'active' },
      { name: 'Child Brand 2', status: 'discontinued' },
    ] 
  }
, { 
    name: 'Parent Brand 2',
    childBrands: [
      { name: 'Child Brand 1', status: 'discontinued' },
      { name: 'Child Brand 2', status: 'active' },
    ] 
  }
];

How do I make it so that it filters the child brands by status and returns the parent object? After filtering by 'active' status it should return something like this,

const array = [
  { 
    name: 'Parent Brand 1', 
    childBrands: [
      { name: 'Child Brand 1', status: 'active' },
    ] 
  }
, { 
    name: 'Parent Brand 2',
    childBrands: [
      { name: 'Child Brand 2', status: 'active' },
    ] 
  }
];

Using a flatMap and filter only returns the child elements when i need the parent object including the child element

{ "name": "Child Brand 1","status": "active" }

{ "name": "Child Brand 2","status": "active" }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Since you haven't posted any code it's hard to know how you would be using flatMap() in this case. You can simply map() over the array and filter each nested array property.

const array = [{ name: 'Parent Brand 1', childBrands: [{ name: 'Child Brand 1', status: 'active' }, { name: 'Child Brand 2', status: 'discontinued' },] }, { name: 'Parent Brand 2', childBrands: [{ name: 'Child Brand 1', status: 'discontinued' }, { name: 'Child Brand 2', status: 'active' },] }];

const filtered = array.map(parent => ({
  ...parent,
  childBrands: parent.childBrands.filter(child => child.status === 'active')
}));

console.log(filtered)

about 4 years ago · Juan Pablo Isaza Report

0

You use Array#map on the major array and Array#filter on the minor arrays as follows:

const array = [ { name: 'Parent Brand 1', childBrands: [ { name: 'Child Brand 1', status: 'active' }, { name: 'Child Brand 2', status: 'discontinued' }, ] } , { name: 'Parent Brand 2', childBrands: [ { name: 'Child Brand 1', status: 'discontinued' }, { name: 'Child Brand 2', status: 'active' }, ] } ],

      output = array.map(
          ({childBrands,...rest}) =>
          ({
              ...rest,
              childBrands:childBrands.filter(
                  ({status}) => status === 'active'
              )
          })
      );
      
console.log( output );

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!