Last active
June 28, 2024 02:07
-
-
Save raggesilver/38d2d9daafcf8f7864d594f241ee2163 to your computer and use it in GitHub Desktop.
A JavaScript function to remove null, undefined, empty strings, empty arrays from an object, recursively
This file contains 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
/** | |
* MIT License | |
* | |
* Copyright (c) 2024 Paulo Queiroz | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to | |
* deal in the Software without restriction, including without limitation the | |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
* sell copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice (including the next | |
* paragraph) shall be included in all copies or substantial portions of the | |
* Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
/** | |
* Recursively remove any properties/array elements that are null, undefined, or | |
* empty strings. Additionally, if an array is empty, remove it. | |
*/ | |
function removeFalsyKeys(obj) { | |
const isArray = obj instanceof Array; | |
const newArray = []; | |
for (const key in obj) { | |
// If the value is null, undefined, or an empty string, remove it. | |
if (obj[key] === null || obj[key] === undefined || obj[key] === "") { | |
// On arrays we don't delete the key, we just skip it. For a value to make | |
// it to an array, it must pass all the checks and be pushed onto | |
// `newArray`. | |
if (!isArray) { | |
delete obj[key]; | |
} | |
} | |
// On elements that are objects, we recursively call `removeNulls` to remove | |
// any null, undefined, or empty string values. | |
else if (typeof obj[key] === "object") { | |
const newObj = removeFalsyKeys(obj[key]); | |
// If obj[key] is an array and it's empty, remove it. | |
if (newObj instanceof Array && newObj.length === 0) { | |
if (!isArray) { | |
delete obj[key]; | |
} | |
} | |
// Otherwise we push the new object onto the array | |
else if (isArray) { | |
newArray.push(newObj); | |
} | |
// Or replace the old object with the new object. | |
else { | |
obj[key] = newObj; | |
} | |
} | |
// If all checks passed and we are dealing with an array, push the value onto | |
// `newArray`. | |
else if (isArray) { | |
newArray.push(obj[key]); | |
} | |
} | |
return isArray ? newArray : obj; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example:
Output: