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.

29 lines
988 B

4 years ago
  1. import reduceBy from "./reduceBy.js";
  2. /**
  3. * Given a function that generates a key, turns a list of objects into an
  4. * object indexing the objects by the given key. Note that if multiple
  5. * objects generate the same value for the indexing key only the last value
  6. * will be included in the generated object.
  7. *
  8. * Acts as a transducer if a transformer is given in list position.
  9. *
  10. * @func
  11. * @memberOf R
  12. * @since v0.19.0
  13. * @category List
  14. * @sig (a -> String) -> [{k: v}] -> {k: {k: v}}
  15. * @param {Function} fn Function :: a -> String
  16. * @param {Array} array The array of objects to index
  17. * @return {Object} An object indexing each array element by the given property.
  18. * @example
  19. *
  20. * const list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
  21. * R.indexBy(R.prop('id'), list);
  22. * //=> {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}}
  23. */
  24. var indexBy =
  25. /*#__PURE__*/
  26. reduceBy(function (acc, elem) {
  27. return elem;
  28. }, null);
  29. export default indexBy;