indexBy.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  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. * @typedefn Idx = String | Int | Symbol
  15. * @sig Idx a => (b -> a) -> [b] -> {a: b}
  16. * @param {Function} fn Function :: a -> Idx
  17. * @param {Array} array The array of objects to index
  18. * @return {Object} An object indexing each array element by the given property.
  19. * @see R.groupBy
  20. * @example
  21. *
  22. * const list = [{id: 'xyz', title: 'A'}, {id: 'abc', title: 'B'}];
  23. * R.indexBy(R.prop('id'), list);
  24. * //=> {abc: {id: 'abc', title: 'B'}, xyz: {id: 'xyz', title: 'A'}}
  25. */
  26. var indexBy =
  27. /*#__PURE__*/
  28. reduceBy(function (acc, elem) {
  29. return elem;
  30. }, null);
  31. export default indexBy;