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

188
Visualizações
How to return only non-null values ​of object properties in an object array?

I want to receive a single object array and return only the non-null values ​​in the object arrays.

However, the method I have implemented contains non-null properties in an array and returns them as an array. How can I make it easier?

I want it to be returned as an object, not as an array.

const arr = [{
text01 : 'name',
text02 : 'email@gmail.com',
text03 : '010-1234-5678',
text04 : 'adress1',
text05 : 'adress2',
text06 : null,
text07 : null,
text08 : null,
},
{
text01 : 'name1',
text02 : 'email2@gmail.com',
text03 : '010-1255-5148',
text04 : 'adress3',
text05 : 'adress4',
text06 : null,
text07 : null,
text08 : null,
}]    
getDataArr(arr) {
            arr.forEach(item => {
                const aaa = [];
                for (let key in item) {
                    if (item[key] !== null) {
                        const value = item[key];
                        aaa.push({ key, value });
                    }
                }
                console.log(aaa);
            });

        // Get the value as const arr = [{
text01 : 'name',
text02 : 'email@gmail.com',
text03 : '010-1234-5678',
text04 : 'adress1',
text05 : 'adress2',
},
{
text01 : 'name1',
text02 : 'email2@gmail.com',
text03 : '010-1255-5148',
text04 : 'adress3',
text05 : 'adress4',
}]    
        },
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You could filter empty objects, undefined and null values from an array of objects like this:

const data = [
  {name: "joe", age:99},
  {name: "jack", age:8},
  undefined,
  {name: "lola", age:54},
  {},
  null
]

function getValidArray(arr){
  return arr.filter(obj=> {
    if(!!obj && typeof obj === "object"){
      return Object.keys(obj).length > 0
    }
    return !!obj
  })
}

console.log(getValidArray(data))

EDIT:

you can achieve the desired result of your updated thread like this:

const arr = [{
text01 : 'name',
text02 : 'email@gmail.com',
text03 : '010-1234-5678',
text04 : 'adress1',
text05 : 'adress2',
text06 : null,
text07 : null,
text08 : null,
},
{
text01 : 'name1',
text02 : 'email2@gmail.com',
text03 : '010-1255-5148',
text04 : 'adress3',
text05 : 'adress4',
text06 : null,
text07 : null,
text08 : null,
}]    

function removeNullEntries(arr){
  return arr.map(obj=> {
    const entries = Object.entries(obj)
    const validEntries = entries.filter(([key, value])=> !!value)
    return Object.fromEntries(validEntries)
  })
}

console.log(removeNullEntries(arr))

about 4 years ago · Juan Pablo Isaza Relatório

0

You can easily achieve the result using filter and map as a single-liner:

arr.map((obj) =>
  Object.fromEntries(Object.entries(obj).filter(([k, v]) => v !== null))
);

const arr = [
  {
    text01: 'name',
    text02: 'email@gmail.com',
    text03: '010-1234-5678',
    text04: 'adress1',
    text05: 'adress2',
    text06: null,
    text07: null,
    text08: null,
  },
  {
    text01: 'name1',
    text02: 'email2@gmail.com',
    text03: '010-1255-5148',
    text04: 'adress3',
    text05: 'adress4',
    text06: null,
    text07: null,
    text08: null,
  },
];

const result = arr.map((obj) => Object.fromEntries(Object.entries(obj).filter(([k, v]) => v !== null)));

console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

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