Skip to content

Instantly share code, notes, and snippets.

@oleggrishechkin
Created November 24, 2020 16:26
Show Gist options
  • Save oleggrishechkin/1bf883fedfeef7b33f9a043ff4446d09 to your computer and use it in GitHub Desktop.
Save oleggrishechkin/1bf883fedfeef7b33f9a043ff4446d09 to your computer and use it in GitHub Desktop.
const stringCheck = (keyToOmit) => (value, key) => keyToOmit === key;
const arrayOfStringCheck = (keysToOmit) => (value, key) => keysToOmit.includes(key);
const omit = (targetObject, options) => {
const check = typeof options === 'function' ? options : Array.isArray(options) ? arrayOfStringCheck : stringCheck;
return Object.keys(targetObject).reduce((result, key) => {
if (!check(targetObject[key], key, targetObject)) {
result[key] = targetObject[key];
}
return result;
}, {});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment