Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda