Created
April 18, 2020 11:36
-
-
Save robertdempsey/6f1d867b508fb0ec4ca5c8f289fb3ab8 to your computer and use it in GitHub Desktop.
Supports omitting multiple properties, at the top level only
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) { | |
delete 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: { | |
* some: 1, // NOTE: this property has NOT been omitted, as this function does not support flattened paths | |
* useful: 2, | |
* data: 3 | |
* } | |
* } | |
*/ | |
console.log(objectFromFrontend) | |
/** | |
* { | |
* _id: 5, | |
* data: { | |
* some: 1, | |
* useful: 2, | |
* data: 3 | |
* } | |
* } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment