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

119
Visualizações
How to get only non-null value of object property from object array?

In the object array received from the server, I want to process and retrieve only the non-null value of the property in the object. How can I change it in my function??

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
3 Respostas
Responde à pergunta

0

A concise way to remove null values is to filter object entries, creating an object from those remaining. With that, just map over the input array...

function nonNullValues(obj) {
  return Object.fromEntries(
    Object.entries(obj).filter(([key, value]) => value !== null)
  )
}
const result = theArray().map(nonNullValues);
console.log(result)

function theArray() {
  return [{
      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,
    }
  ]
}

about 4 years ago · Juan Pablo Isaza Relatório

0

Lodash if you don't mind. Using: ominBy and isNull

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 => _.omitBy(obj, _.isNull));

console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório

0

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 nonNull = []
for(const item of arr) {
    const obj = {}
    for(const key in item) {
        if(item[key]) obj[key] = item[key]
    }
    nonNull.push(obj)
}
console.log(nonNull)

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