Last active
April 8, 2020 11:21
-
-
Save robertdempsey/5ca142cf8bbb72315b9e12898fc18bc9 to your computer and use it in GitHub Desktop.
Uses Lodash 'pickBy' to omit properties whose keys do not match those we've specified.
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: object, keys: string[]) => _.pickBy(objectFromFrontend, (value, key) => !keys.includes(key)) | |
const objectFromFrontend = { | |
_id: 5, | |
data: { | |
some: 1, | |
useful: 2, | |
data: 3 | |
} | |
} | |
const objectToInsertIntoDB = omit(objectFromFrontend, ['_id', 'data.some']); | |
console.log(objectToInsertIntoDB) | |
/** | |
* { | |
* data: { | |
* some: 1, | |
* 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