Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!