pick.js 935 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import _curry2 from "./internal/_curry2.js";
  2. /**
  3. * Returns a partial copy of an object containing only the keys specified. If
  4. * the key does not exist, the property is ignored.
  5. *
  6. * @func
  7. * @memberOf R
  8. * @since v0.1.0
  9. * @category Object
  10. * @sig [k] -> {k: v} -> {k: v}
  11. * @param {Array} names an array of String property names to copy onto a new object
  12. * @param {Object} obj The object to copy from
  13. * @return {Object} A new object with only properties from `names` on it.
  14. * @see R.omit, R.props
  15. * @example
  16. *
  17. * R.pick(['a', 'd'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1, d: 4}
  18. * R.pick(['a', 'e', 'f'], {a: 1, b: 2, c: 3, d: 4}); //=> {a: 1}
  19. */
  20. var pick =
  21. /*#__PURE__*/
  22. _curry2(function pick(names, obj) {
  23. var result = {};
  24. var idx = 0;
  25. while (idx < names.length) {
  26. if (names[idx] in obj) {
  27. result[names[idx]] = obj[names[idx]];
  28. }
  29. idx += 1;
  30. }
  31. return result;
  32. });
  33. export default pick;