Last active
April 8, 2020 10:55
-
-
Save robertdempsey/36f460326bcd40a02e5bd376c0448054 to your computer and use it in GitHub Desktop.
An alternative to Lodash omit, which allows you to omit a single property.
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 = (keyToOmit: string, { [keyToOmit]: _, ...omittedPropObj } = {}) => omittedPropObj; | |
const objectFromFrontend = { | |
_id: 5, | |
data: { | |
some: 1, | |
useful: 2, | |
data: 3 | |
} | |
} | |
const objectToInsertIntoDB = omit('_id', objectFromFrontend); | |
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