Last active
April 11, 2020 11:04
-
-
Save robertdempsey/6de3a5161beda4a15c979519ebf2394a to your computer and use it in GitHub Desktop.
A native omit function that allows you to specify multiple properties to omit.
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 = (keysToOmit: string[], originalObj = {}) => | |
Object.fromEntries( | |
Object.entries(originalObj) | |
.filter(([key]) => !keysToOmit.includes(key)) | |
) | |
const objectFromFrontend = { | |
_id: 5, | |
data: { | |
some: 1, | |
useful: 2, | |
data: 3 | |
} | |
} | |
const objectToInsertIntoDB = omit(['_id', 'data.some'], objectFromFrontend); | |
console.log(objectToInsertIntoDB) | |
/** | |
* { | |
* data: { | |
* some: 1, !!! has not been removed | |
* 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