Last active
June 23, 2017 03:49
-
-
Save chriswrightdesign/19eedff000e122c754ccad8bacb66c03 to your computer and use it in GitHub Desktop.
Reduce objects
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 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