You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.2 KiB

4 years ago
  1. import _curry2 from "./internal/_curry2.js";
  2. import map from "./map.js";
  3. import prop from "./prop.js";
  4. /**
  5. * Returns a new list by plucking the same named property off all objects in
  6. * the list supplied.
  7. *
  8. * `pluck` will work on
  9. * any [functor](https://github.com/fantasyland/fantasy-land#functor) in
  10. * addition to arrays, as it is equivalent to `R.map(R.prop(k), f)`.
  11. *
  12. * @func
  13. * @memberOf R
  14. * @since v0.1.0
  15. * @category List
  16. * @sig Functor f => k -> f {k: v} -> f v
  17. * @param {Number|String} key The key name to pluck off of each object.
  18. * @param {Array} f The array or functor to consider.
  19. * @return {Array} The list of values for the given key.
  20. * @see R.props
  21. * @example
  22. *
  23. * var getAges = R.pluck('age');
  24. * getAges([{name: 'fred', age: 29}, {name: 'wilma', age: 27}]); //=> [29, 27]
  25. *
  26. * R.pluck(0, [[1, 2], [3, 4]]); //=> [1, 3]
  27. * R.pluck('val', {a: {val: 3}, b: {val: 5}}); //=> {a: 3, b: 5}
  28. * @symb R.pluck('x', [{x: 1, y: 2}, {x: 3, y: 4}, {x: 5, y: 6}]) = [1, 3, 5]
  29. * @symb R.pluck(0, [[1, 2], [3, 4], [5, 6]]) = [1, 3, 5]
  30. */
  31. var pluck =
  32. /*#__PURE__*/
  33. _curry2(function pluck(p, list) {
  34. return map(prop(p), list);
  35. });
  36. export default pluck;