Skip to content

Instantly share code, notes, and snippets.

@robertdempsey
Created April 18, 2020 11:36
Show Gist options
  • Save robertdempsey/6f1d867b508fb0ec4ca5c8f289fb3ab8 to your computer and use it in GitHub Desktop.
Save robertdempsey/6f1d867b508fb0ec4ca5c8f289fb3ab8 to your computer and use it in GitHub Desktop.
Supports omitting multiple properties, at the top level only
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