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
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 changedrecursive
- set this to true
to recursively process your objectTherefore, for your use case, you can run:
rundef(object, false, true)