Skip to content

Instantly share code, notes, and snippets.

@robertdempsey
Created April 18, 2020 10:53
Show Gist options
  • Save robertdempsey/30107fc160a66cfcd8c08d75915b8ac6 to your computer and use it in GitHub Desktop.
Save robertdempsey/30107fc160a66cfcd8c08d75915b8ac6 to your computer and use it in GitHub Desktop.
Allows flattened paths to be omitted and does not modify our original object
const omit = (originalObject = {}, keysToOmit = []) => {
const clonedObject = _.cloneDeep(originalObject);
for (const path of keysToOmit) {
_.unset(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: {
* 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