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

133
Vistas
How to get array elements based on conditions using javascript array reduce?

So I have an array

const records = [
{
    value: 24,
    gender: "BOYS"
},
{
    value: 42,
    gender: "BOYS"
},
{
    value: 85,
    gender: "GIRLS"
},
{
    value: 12,
    gender: "GIRLS"
},
{
    value: 10,
    gender: "BOYS"
}

]

And I want to get only "Boys" objects within an array using js reduce() rather than filter(). Please help.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

const records = [
{
    value: 24,
    gender: "BOYS"
},
{
    value: 42,
    gender: "BOYS"
},
{
    value: 85,
    gender: "GIRLS"
},
{
    value: 12,
    gender: "GIRLS"
},
{
    value: 10,
    gender: "BOYS"
}

]

let boys = records.reduce((t,o)=>{
  if(o.gender === "BOYS") t.push(o)
  return t
},[])
console.log(boys)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Уou can use any option you want

const records = [{value: 24,gender: "BOYS"},{value: 42,gender: "BOYS"},{value: 85,gender: "GIRLS"},{value: 12,gender: "GIRLS"},{value: 10,gender: "BOYS"}]

const withReduce = records.reduce((acc, item) => item.gender === "BOYS" ? [...acc, item] : acc, []);
const withFlatMap = records.flatMap(item => item.gender === "BOYS" ? item : []);
const withFilter = records.filter(({ gender }) => gender === "BOYS");

console.log(withReduce);
console.log(withFlatMap);
console.log(withFilter);
.as-console-wrapper { max-height: 100% !important; top: 0 }

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