Skip to content

Instantly share code, notes, and snippets.

@robertdempsey
Last active April 8, 2020 11:21
Show Gist options
  • Save robertdempsey/5ca142cf8bbb72315b9e12898fc18bc9 to your computer and use it in GitHub Desktop.
Save robertdempsey/5ca142cf8bbb72315b9e12898fc18bc9 to your computer and use it in GitHub Desktop.
Uses Lodash 'pickBy' to omit properties whose keys do not match those we've specified.
const omit = (originalObject: object, keys: string[]) => _.pickBy(objectFromFrontend, (value, key) => !keys.includes(key))
const objectFromFrontend = {
_id: 5,
data: {
some: 1,
useful: 2,
data: 3
}
}
const objectToInsertIntoDB = omit(objectFromFrontend, ['_id', 'data.some']);
console.log(objectToInsertIntoDB)
/**
* {
* data: {
* some: 1,
* 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