Last active
June 27, 2018 12:39
-
-
Save vaidd4/e9d9dabc672a9c753e5f6eed3211f6d9 to your computer and use it in GitHub Desktop.
Pick the specified properties of an object
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
| /** | |
| * 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