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.
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)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 }