Home > other >  remove null value while destructoring the data in node js / javascript
remove null value while destructoring the data in node js / javascript

Time:02-27

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: "[email protected]",
     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

CodePudding user response:

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: "[email protected]",
     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: "[email protected]",
     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: "[email protected]",
     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)

  • Related