Last active
April 18, 2020 10:45
-
-
Save robertdempsey/8d83d558f4986085c663a409de7675e0 to your computer and use it in GitHub Desktop.
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
const omit = (originalObject = {}, keysToOmit = []) => { | |
const clonedObject = { ...originalObject }; | |
for (const path of keysToOmit) { | |
_.unset(clonedObject, path) | |
} | |
return clonedObject; | |
} | |
const objectFromFrontend = { | |
_id: 5, | |
data: { | |
some: 1, | |
useful: 2, | |
data: 3 | |
} | |
} | |
const objectToInsertIntoDB = omit(objectFromFrontend, ['_id', 'data.some']); | |
console.log(objectToInsertIntoDB) | |
/** | |
* { | |
* data: { | |
* useful: 2, | |
* data: 3 | |
* } | |
* } | |
*/ | |
console.log(objectFromFrontend) | |
/** | |
* { | |
* _id: 5, | |
* data: { | |
* useful: 2, // NOTE: our ORIGINAL object has also had its data.some property removed | |
* data: 3 | |
* } | |
* } | |
*/ |
Thanks. And thanks for reading. I actually just fixed up this gist. There were a few copy and paste errors, and I implemented the for... of loop for improved performance.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Neat stuff, good article, thanks!