Skip to content

Instantly share code, notes, and snippets.

@vaidd4
Last active June 27, 2018 12:39
Show Gist options
  • Select an option

  • Save vaidd4/e9d9dabc672a9c753e5f6eed3211f6d9 to your computer and use it in GitHub Desktop.

Select an option

Save vaidd4/e9d9dabc672a9c753e5f6eed3211f6d9 to your computer and use it in GitHub Desktop.
Pick the specified properties of an object
/**
* Pick the specified properties of an object
*
* @param obj
* @param {Array<Array<string> | string>} keys
* @returns {{}}
*/
function pick (obj, keys) {
return keys.reduce((sum, k) => (Array.isArray(k) ? sum[k[1]] = obj[k[0]] : sum[k] = obj[k], sum), {})
}
/** USAGE */
pick({a: '1', b: '2', c: '3', d: '4'}, ['a', 'c', ['d', 'e']]) // => {a: '1', c: '3', e: '4'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment