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

179
Vistas
How to include data to object if element is not falsy?

I have the map:

map(([reportProperties, reportObjectsProperties, textProperties, visibleText]) => {
                return { reportObjectsProperties, reportProperties, textProperties, visibleText };
            }),

I try to check if all parameters are not falsy include them to result object like this:

map(([reportProperties, reportObjectsProperties, textProperties, visibleText]) => {
                if(visibleText)
                   return { reportObjectsProperties, reportProperties, textProperties, visibleText };
                if(reportObjectsProperties && reportObjectsProperties.length)
                   return { reportObjectsProperties, reportProperties, textProperties, visibleText };

....

            }),

How to make this more elegant?

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

0

Assuming your object is an array, you can check if all values exists using every method. And you don't need map there because you don't do a transformation on your object.

instead of

.map(arr => {
 ... return arr;
});

do

.filter(row => row.every(cell => typeof cell !== undefined && cell !== null));
about 4 years ago · Juan Pablo Isaza Denunciar

0

My suggestion is to look into Array.prototype.filter() method paired with Object.entries to filter out the falsy values. And pair it with a reduce function to reconstruct the new object

I would take this into the following direction:

const arr = [{a: true, b: true, c: false}, {a: false, b: true}]

const excludeKeys = ["c"]


let result = arr.map(obj => {
  return Object.entries(obj).filter(([key, value]) => !excludeKeys.includes(key) && value)
    .reduce((prev, [key, value]) => ({...prev, [key]: value}), {})
})

This provides a functional and pretty generic interface for filtering out the falsy / excluded object key / values.

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