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

591
Visualizações
Javascript - How to remove all nested null or undefined properties and empty objects?

I'm looking to essentially "trim" a JSON object to only objects and properties that have meaningful non-empty data.

I need to write a performant function that takes an object like this:

[Function Input]

{
    "id": "123",
    "firstName": undefined
    "lastName": null
    "contactInfo": {
        "email": "notreal@somedomain.com",
        "contactInfo": {
            "address": {
                "streetAddress1": undefined,
                "state": null
            }
        }
    }
}

[Function Output]

{
    "id": "123",
    "contactInfo": {
        "email": "notreal@somedomain.com"
    }
}

This is what I've tried:

export function removeNullOrUndefinedProperties(obj: any): any {
  return Object.entries(obj)
    .filter(([_, v]) => v != null)
    .reduce((acc, [k, v]) => ({ ...acc, [k]: v === Object(v) ? removeNullOrUndefinedProperties(v) : v }), {});
}

export function removeEmptyObjects(obj: any): any {
  for (const k in obj) {
    if (!obj[k] || typeof obj[k] !== 'object') {
      continue; // If null or not an object, skip to the next iteration
    }

    // The property is an object
    removeEmptyObjects(obj[k]); // <-- Make a recursive call on the nested object
    if (Object.keys(obj[k]).length === 0) {
      delete obj[k]; // The object had no properties, so delete that property
    }
  }
  return obj;
}

Call to the function:

removeEmptyObjects(removeNullOrUndefinedProperties(json));

This solution is slow, and it doesn't always work; specifically, if you pass an array, it returns numbered indexes on the array which is unexpected data. How do I write this function in a correct and performant way?

about 4 years ago · Juan Pablo Isaza
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