mapKeys.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. var baseForOwn = require('./_baseForOwn'),
  2. baseIteratee = require('./_baseIteratee');
  3. /**
  4. * The opposite of `_.mapValues`; this method creates an object with the
  5. * same values as `object` and keys generated by running each own enumerable
  6. * string keyed property of `object` thru `iteratee`. The iteratee is invoked
  7. * with three arguments: (value, key, object).
  8. *
  9. * @static
  10. * @memberOf _
  11. * @since 3.8.0
  12. * @category Object
  13. * @param {Object} object The object to iterate over.
  14. * @param {Array|Function|Object|string} [iteratee=_.identity]
  15. * The function invoked per iteration.
  16. * @returns {Object} Returns the new mapped object.
  17. * @see _.mapValues
  18. * @example
  19. *
  20. * _.mapKeys({ 'a': 1, 'b': 2 }, function(value, key) {
  21. * return key + value;
  22. * });
  23. * // => { 'a1': 1, 'b2': 2 }
  24. */
  25. function mapKeys(object, iteratee) {
  26. var result = {};
  27. iteratee = baseIteratee(iteratee, 3);
  28. baseForOwn(object, function(value, key, object) {
  29. result[iteratee(value, key, object)] = value;
  30. });
  31. return result;
  32. }
  33. module.exports = mapKeys;