在节点 js / javascript 中析构数据时删除空值

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

我正在尝试破坏函数以获取 return 数据并将其存储在 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);
]

所以我得到的输出是

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"
   }
]

在我析构数据时是否有任何可能的方法删除空值我知道我正在return空值但我想在破坏数据时删除空值

想到了 3 种方法

1.过滤掉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.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.otherDetails 中过滤非空值。这也将删除并非来自 arrayObj 但最初出现在 otherDetails

中的 nulls

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)