How to delete recursively undefined properties from an object - while keeping the constructor chain?

前端 未结 4 2446
醉酒成梦
醉酒成梦 2021-02-20 14:01

This is a question similar to How to remove undefined and null values from an object using lodash?. However, the solutions proposed there do not conserve the constructor. In add

4条回答
  •  Happy的楠姐
    2021-02-20 14:29

    You can use the rundef package.

    By default, it will replace all top-level properties with values set to undefined. However it supports the following options:

    • mutate - set this to false to return the same object that you have provided; this will ensure the constructor is not changed
    • recursive - set this to true to recursively process your object

    Therefore, for your use case, you can run:

    rundef(object, false, true)
    

提交回复
热议问题