@wasmuth/pick
Version:
Practical, functional utilities that fallback on native implementations as much as possible
21 lines (18 loc) • 485 B
JavaScript
import check from 'check-arg-types'
import partial from '@wasmuth/partial'
/**
* Return a new object with only the specified keys included.
* @param {Array} keys
* @param {Object} obj
* @return {Object}
*/
export default function pick (keys, obj) {
if (arguments.length < 2) return partial(pick, arguments)
check(arguments, ['array', 'object'])
const result = {}
for (let x = 0; x < keys.length; x++) {
let k = keys[x]
result[k] = obj[k]
}
return result
}