mapKeys.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. var baseAssignValue = require('./_baseAssignValue'),
  2. baseForOwn = require('./_baseForOwn'),
  3. baseIteratee = require('./_baseIteratee');
  4. /**
  5. * The opposite of `_.mapValues`; this method creates an object with the
  6. * same values as `object` and keys generated by running each own enumerable
  7. * string keyed property of `object` thru `iteratee`. The iteratee is invoked
  8. * with three arguments: (value, key, object).
  9. *
  10. * @static
  11. * @memberOf _
  12. * @since 3.8.0
  13. * @category Object
  14. * @param {Object} object The object to iterate over.
  15. * @param {Function} [iteratee=_.identity] 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. baseAssignValue(result, iteratee(value, key, object), value);
  30. });
  31. return result;
  32. }
  33. module.exports = mapKeys;