Skip to content

Instantly share code, notes, and snippets.

@robertdempsey
Last active April 18, 2020 10:45
Show Gist options
  • Save robertdempsey/8d83d558f4986085c663a409de7675e0 to your computer and use it in GitHub Desktop.
Save robertdempsey/8d83d558f4986085c663a409de7675e0 to your computer and use it in GitHub Desktop.
const omit = (originalObject = {}, keysToOmit = []) => {
const clonedObject = { ...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: {
* useful: 2, // NOTE: our ORIGINAL object has also had its data.some property removed
* data: 3
* }
* }
*/
@neenjaw
Copy link

neenjaw commented Apr 13, 2020

Neat stuff, good article, thanks!

@robertdempsey
Copy link
Author

Thanks. And thanks for reading. I actually just fixed up this gist. There were a few copy and paste errors, and I implemented the for... of loop for improved performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment