Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda