JS过滤对象中值为null的空对象

 要过滤对象中值为 null 的属性,可以使用 Object.entries() 方法来遍历对象的键值对。然后使用 Array.prototype.reduce() 方法创建一个新的对象,只包含值不为 null 的属性。

const patientIntervene = {
  createBy: null,
  createTime: null,
  id: null,
  interveneContent: "01",
  interveneName: "01",
  interveneUrl: "01",
  patientId: null,
  routeDetailId: null,
  serviceTemplateId: null,
  teamId: null,
  updateBy: null,
  updateTime: null
};

const filteredObject = Object.entries(patientIntervene).reduce((acc, [key, value]) => {
  if (value !== null) {
    acc[key] = value;
  }
  return acc;
}, {});

console.log(filteredObject);

//输出结果
{
  interveneContent: "01",
  interveneName: "01",
  interveneUrl: "01"
}

我们使用 Object.entries(patientIntervene) 获取对象中的键值对数组。然后使用 reduce() 方法遍历该数组,初始值为一个空对象 {}

在每次迭代中,如果值 value 不为 null,则将该键值对添加到累加器对象 acc 中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值