Skip to content

Instantly share code, notes, and snippets.

@robertdempsey
Last active April 8, 2020 10:55
Show Gist options
  • Save robertdempsey/36f460326bcd40a02e5bd376c0448054 to your computer and use it in GitHub Desktop.
Save robertdempsey/36f460326bcd40a02e5bd376c0448054 to your computer and use it in GitHub Desktop.
An alternative to Lodash omit, which allows you to omit a single property.
const omit = (keyToOmit: string, { [keyToOmit]: _, ...omittedPropObj } = {}) => omittedPropObj;
const objectFromFrontend = {
_id: 5,
data: {
some: 1,
useful: 2,
data: 3
}
}
const objectToInsertIntoDB = omit('_id', objectFromFrontend);
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