Last active
August 14, 2019 10:14
-
-
Save pete-otaqui/ce7dceddf18cbcad6fc60965e4ae9794 to your computer and use it in GitHub Desktop.
Filter out keys from an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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