Skip to content

Instantly share code, notes, and snippets.

@pete-otaqui
Last active August 14, 2019 10:14
Show Gist options
  • Save pete-otaqui/ce7dceddf18cbcad6fc60965e4ae9794 to your computer and use it in GitHub Desktop.
Save pete-otaqui/ce7dceddf18cbcad6fc60965e4ae9794 to your computer and use it in GitHub Desktop.
Filter out keys from an object
const filterKeys = (obj, keys) =>
Object.entries(obj).reduce(
(acc, [k, v]) => (keys.includes(k) ? acc : { ...acc, [k]: v }),
{}
);
/**
// Usage:
const myObject = { foo: 'foo', bar: 'bar', baz: 'baz', eck: 'eck' };
const filteredObject = filterKeys(myObject, ['foo', 'eck']);
// { bar: 'bar', baz: 'baz' };
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment