Skip to content

Instantly share code, notes, and snippets.

@chriswrightdesign
Last active June 23, 2017 03:49
Show Gist options
  • Save chriswrightdesign/19eedff000e122c754ccad8bacb66c03 to your computer and use it in GitHub Desktop.
Save chriswrightdesign/19eedff000e122c754ccad8bacb66c03 to your computer and use it in GitHub Desktop.
Reduce objects
const obj = {
name: 'Kuro',
species: 'dog',
id: '135',
country: 'Australia'
};
const reject = (...rejectedKeys) => obj =>
// convert the keys of the object to an array and reduce over them
Object.keys(obj).reduce((acc, curr) =>
// if they key is not in the rejected Keys list, append it to the object,
// if it is, just return the accumulator instead
!rejectedKeys.includes(curr) ? {...acc, [curr]: obj[curr]} : {...acc}, {});
console.log(reject('species', 'id')(obj));
/**
Result:
{
name: "Kuro",
country: "Australia"
}
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment