mapValues.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var createObjectMapper = require('../internal/createObjectMapper');
  2. /**
  3. * Creates an object with the same keys as `object` and values generated by
  4. * running each own enumerable property of `object` through `iteratee`. The
  5. * iteratee function is bound to `thisArg` and invoked with three arguments:
  6. * (value, key, object).
  7. *
  8. * If a property name is provided for `iteratee` the created `_.property`
  9. * style callback returns the property value of the given element.
  10. *
  11. * If a value is also provided for `thisArg` the created `_.matchesProperty`
  12. * style callback returns `true` for elements that have a matching property
  13. * value, else `false`.
  14. *
  15. * If an object is provided for `iteratee` the created `_.matches` style
  16. * callback returns `true` for elements that have the properties of the given
  17. * object, else `false`.
  18. *
  19. * @static
  20. * @memberOf _
  21. * @category Object
  22. * @param {Object} object The object to iterate over.
  23. * @param {Function|Object|string} [iteratee=_.identity] The function invoked
  24. * per iteration.
  25. * @param {*} [thisArg] The `this` binding of `iteratee`.
  26. * @returns {Object} Returns the new mapped object.
  27. * @example
  28. *
  29. * _.mapValues({ 'a': 1, 'b': 2 }, function(n) {
  30. * return n * 3;
  31. * });
  32. * // => { 'a': 3, 'b': 6 }
  33. *
  34. * var users = {
  35. * 'fred': { 'user': 'fred', 'age': 40 },
  36. * 'pebbles': { 'user': 'pebbles', 'age': 1 }
  37. * };
  38. *
  39. * // using the `_.property` callback shorthand
  40. * _.mapValues(users, 'age');
  41. * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
  42. */
  43. var mapValues = createObjectMapper();
  44. module.exports = mapValues;