mapValues.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var baseAssignValue = require('./_baseAssignValue'),
  2. baseForOwn = require('./_baseForOwn'),
  3. baseIteratee = require('./_baseIteratee');
  4. /**
  5. * Creates an object with the same keys as `object` and values generated
  6. * by running each own enumerable string keyed property of `object` thru
  7. * `iteratee`. The iteratee is invoked with three arguments:
  8. * (value, key, object).
  9. *
  10. * @static
  11. * @memberOf _
  12. * @since 2.4.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 _.mapKeys
  18. * @example
  19. *
  20. * var users = {
  21. * 'fred': { 'user': 'fred', 'age': 40 },
  22. * 'pebbles': { 'user': 'pebbles', 'age': 1 }
  23. * };
  24. *
  25. * _.mapValues(users, function(o) { return o.age; });
  26. * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
  27. *
  28. * // The `_.property` iteratee shorthand.
  29. * _.mapValues(users, 'age');
  30. * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
  31. */
  32. function mapValues(object, iteratee) {
  33. var result = {};
  34. iteratee = baseIteratee(iteratee, 3);
  35. baseForOwn(object, function(value, key, object) {
  36. baseAssignValue(result, key, iteratee(value, key, object));
  37. });
  38. return result;
  39. }
  40. module.exports = mapValues;