Forked from ConsciousUniverse/gist:8210e210f3cfab0200450db5a91c5ccc
Created
February 28, 2020 20:01
-
-
Save snobu/7dbee9a5fc43c6c664b73409bb2f7f88 to your computer and use it in GitHub Desktop.
Javascript (Typescript): Remove empty properties from object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| deleteEmptyProps(obj: any): any { | |
| // modifies passed obj in place, removing empty properties (inc. empty arrays) | |
| return Object.keys(obj).forEach(k => { | |
| if (!obj[k] || obj[k] === undefined || | |
| Array.isArray(obj[k]) && obj[k].length === 0) { | |
| delete obj[k]; | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
let obj = {
a: 0,
b: false,
c: '',
d: [],
};
deleteEmptyProps(obj) // obj = {e: 333}
a = 0 // It should also be a valid value