Skip to content

Instantly share code, notes, and snippets.

@robertdempsey
Last active April 11, 2020 11:04
Show Gist options
  • Save robertdempsey/6de3a5161beda4a15c979519ebf2394a to your computer and use it in GitHub Desktop.
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.
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