UNPKG

1.34 kBJavaScriptView Raw
1import baseAssignValue from './_baseAssignValue.js';
2import baseForOwn from './_baseForOwn.js';
3import baseIteratee from './_baseIteratee.js';
4
5/**
6 * Creates an object with the same keys as `object` and values generated
7 * by running each own enumerable string keyed property of `object` thru
8 * `iteratee`. The iteratee is invoked with three arguments:
9 * (value, key, object).
10 *
11 * @static
12 * @memberOf _
13 * @since 2.4.0
14 * @category Object
15 * @param {Object} object The object to iterate over.
16 * @param {Function} [iteratee=_.identity] The function invoked per iteration.
17 * @returns {Object} Returns the new mapped object.
18 * @see _.mapKeys
19 * @example
20 *
21 * var users = {
22 * 'fred': { 'user': 'fred', 'age': 40 },
23 * 'pebbles': { 'user': 'pebbles', 'age': 1 }
24 * };
25 *
26 * _.mapValues(users, function(o) { return o.age; });
27 * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
28 *
29 * // The `_.property` iteratee shorthand.
30 * _.mapValues(users, 'age');
31 * // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
32 */
33function mapValues(object, iteratee) {
34 var result = {};
35 iteratee = baseIteratee(iteratee, 3);
36
37 baseForOwn(object, function(value, key, object) {
38 baseAssignValue(result, key, iteratee(value, key, object));
39 });
40 return result;
41}
42
43export default mapValues;