Skip to content

Instantly share code, notes, and snippets.

@jasonphillips
Last active May 11, 2017 16:08
Show Gist options
  • Save jasonphillips/a2a4253c1af96af7ddb3cb2dc05c41f1 to your computer and use it in GitHub Desktop.
Save jasonphillips/a2a4253c1af96af7ddb3cb2dc05c41f1 to your computer and use it in GitHub Desktop.
Pick "One"-Liner
const pick = (obj, props) => Array.prototype.reduce.call(
props,
(built, prop) => (
Object.prototype.hasOwnProperty.call(obj, prop)
? Object.assign({}, built, {[prop]: obj[prop]})
: built
), {}
)
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