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

131
Views
¿Cómo obtener elementos de matriz basados en condiciones usando javascript array reduce?

Entonces tengo una matriz

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

]

Y quiero obtener solo objetos "Boys" dentro de una matriz usando js reduce() en lugar de filter(). Por favor ayuda.

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

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 Report

0

Puedes usar cualquier opción que quieras

 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 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!