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

172
Visualizações
remove null value while destructoring the data in node js / javascript

I am trying to destructing the function to fetch the return data and store it in and Array

id: data.id,
title: data.title,
otherDetails:[
   {
     name:"dummy name",
     fields: "testing",
     production: "not yet",
   },
   {
     add:"New York",
     core: "mech",
     position: "junior",
   },
   ...arrayObj(data);
]

so I am getting the output as

id: data.id,
title: data.title,
otherDetails:[
   {
     name:"dummy name",
     fields: "testing",
     production: "not yet",
   },
   {
     add:"New York",
     core: "mech",
     position: "junior",
   },
   null,
   {
     user_id: "testing@user.com",
     user_name: "firstname",
   },
   null,
   {
     userschema: "personal",
     access: "yes",
   }
   {
     nominie: "testing",
     age: "18"
   }
]

Is there any possible way when I destructor the data the null value get removed I know I am returning the null value but I want to remove the Null value when I am destructing the data

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

0

3 methods come to mind

1. Filtering out non nulls from the returned array in arrayObj(data)

const data = {
    id: 'data ID',
  title: 'data Title',
  d: [   null,
   {
     user_id: "testing@user.com",
     user_name: "firstname",
   },
   null,
   {
     userschema: "personal",
     access: "yes",
   },
   {
     nominie: "testing",
     age: "18"
   }]
}

const arrayObj = (data) => {
  return data.d
}

const a = {
id: data.id,
title: data.title,
otherDetails:[
   {
     name:"dummy name",
     fields: "testing",
     production: "not yet",
   },
   {
     add:"New York",
     core: "mech",
     position: "junior",
   },
   ...arrayObj(data).filter((el) => el!==null)
]
}

console.log(a)

2. Returing the filtered non null values from arrayObj(data)

const data = {
    id: 'data ID',
  title: 'data Title',
  d: [   null,
   {
     user_id: "testing@user.com",
     user_name: "firstname",
   },
   null,
   {
     userschema: "personal",
     access: "yes",
   },
   {
     nominie: "testing",
     age: "18"
   }]
}

const arrayObj = (data) => {
    return data.d.filter((el) => el!==null)
}

const a = {
id: data.id,
title: data.title,
otherDetails:[
   {
     name:"dummy name",
     fields: "testing",
     production: "not yet",
   },
   {
     add:"New York",
     core: "mech",
     position: "junior",
   },
   ...arrayObj(data)
]
}

console.log(a)

3. Filtering non null values from otherDetails. This will also remove nulls that didn't come from arrayObj but was initially present in otherDetails

const data = {
    id: 'data ID',
  title: 'data Title',
  d: [   null,
   {
     user_id: "testing@user.com",
     user_name: "firstname",
   },
   null,
   {
     userschema: "personal",
     access: "yes",
   },
   {
     nominie: "testing",
     age: "18"
   }]
}

const arrayObj = (data) => {
return data.d
}

const a = {
id: data.id,
title: data.title,
otherDetails:[
   {
     name:"dummy name",
     fields: "testing",
     production: "not yet",
   },
   null,
   {
     add:"New York",
     core: "mech",
     position: "junior",
   },
   ...arrayObj(data)
].filter((el) => el !== null)
}

console.log(a)

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