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

171
Visualizações
Return only the values that are not empty string or empty arrays from an object

Until now I have the following function:

export const useClearEmptyAttributes = (
  obj: { [s: string]: unknown } | ArrayLike<unknown>,
) => {
  return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v));
};

enter image description here

It eliminates an empty field "phone number" which was empty, but not socialLinks (which is an array).

How can I modify my function to eliminate empty arrays too, not just empty strings?

Thank you!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

since you are targeting empty strings and empty arrays and both have length property you can check for the length if it is bigger than 0:

export const useClearEmptyAttributes = (
  obj: { [s: string]: unknown } | ArrayLike<unknown>,
) => {
  return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v.length > 0));
};
about 4 years ago · Juan Pablo Isaza Relatório

0

This would work if you want to add checking empty arrays plus your current check:

export const useClearEmptyAttributes = (
  obj: { [s: string]: unknown } | ArrayLike<unknown>,
) => {
  return Object.fromEntries(Object.entries(obj).filter(([_, v]) => Array.isArray(v)?v.length:v));
};

But if you want to check also for empty objects, etc it can be further modified.

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