Last active
May 11, 2017 16:08
-
-
Save jasonphillips/a2a4253c1af96af7ddb3cb2dc05c41f1 to your computer and use it in GitHub Desktop.
Pick "One"-Liner
This file contains hidden or 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 pick = (obj, props) => Array.prototype.reduce.call( | |
props, | |
(built, prop) => ( | |
Object.prototype.hasOwnProperty.call(obj, prop) | |
? Object.assign({}, built, {[prop]: obj[prop]}) | |
: built | |
), {} | |
) |
This file contains hidden or 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 myObject = {a:'1', b:'2', c:'3', d:'4'} | |
pick(myObject, ['b','d']) | |
// Object {b: "2", d: "4"} | |
pick(myObject, ['a','x']) | |
// Object {a: "1"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment