Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda