SIGN IN SIGN UP

A modern JavaScript utility library delivering modularity, performance, & extras.

0 0 50 JavaScript
2016-09-17 22:24:52 -07:00
define(['./_baseAssignValue', './_baseForOwn', './_baseIteratee'], function(baseAssignValue, baseForOwn, baseIteratee) {
2015-12-16 17:53:20 -08:00
/**
2016-04-01 23:28:48 -07:00
* Creates an object with the same keys as `object` and values generated
* by running each own enumerable string keyed property of `object` thru
2016-03-26 00:00:01 -07:00
* `iteratee`. The iteratee is invoked with three arguments:
* (value, key, object).
2015-12-16 17:53:20 -08:00
*
* @static
* @memberOf _
2016-03-26 00:00:01 -07:00
* @since 2.4.0
2015-12-16 17:53:20 -08:00
* @category Object
* @param {Object} object The object to iterate over.
2016-07-24 09:52:04 -07:00
* @param {Function} [iteratee=_.identity] The function invoked per iteration.
2015-12-16 17:53:20 -08:00
* @returns {Object} Returns the new mapped object.
2016-04-19 22:17:16 -07:00
* @see _.mapKeys
2015-12-16 17:53:20 -08:00
* @example
*
* var users = {
* 'fred': { 'user': 'fred', 'age': 40 },
* 'pebbles': { 'user': 'pebbles', 'age': 1 }
* };
*
* _.mapValues(users, function(o) { return o.age; });
* // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
*
2016-02-01 23:13:04 -08:00
* // The `_.property` iteratee shorthand.
2015-12-16 17:53:20 -08:00
* _.mapValues(users, 'age');
* // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
*/
function mapValues(object, iteratee) {
var result = {};
iteratee = baseIteratee(iteratee, 3);
baseForOwn(object, function(value, key, object) {
2016-09-17 22:24:52 -07:00
baseAssignValue(result, key, iteratee(value, key, object));
2015-12-16 17:53:20 -08:00
});
return result;
}
return mapValues;
});