Files
lodash/mapValue.js
John-David Dalton aa5e1b2fe0 Fix object coercion.
2017-04-16 15:30:42 -05:00

34 lines
945 B
JavaScript

/**
* Creates an object with the same keys as `object` and values generated
* by running each own enumerable string keyed property of `object` thru
* `iteratee`. The iteratee is invoked with three arguments:
* (value, key, object).
*
* @since 2.4.0
* @category Object
* @param {Object} object The object to iterate over.
* @param {Function} iteratee The function invoked per iteration.
* @returns {Object} Returns the new mapped object.
* @see mapKeys
* @example
*
* const users = {
* 'fred': { 'user': 'fred', 'age': 40 },
* 'pebbles': { 'user': 'pebbles', 'age': 1 }
* }
*
* mapValue(users, ({ age }) => age)
* // => { 'fred': 40, 'pebbles': 1 } (iteration order is not guaranteed)
*/
function mapValue(object, iteratee) {
object = Object(object)
const result = {}
Object.keys(object).forEach((key) => {
result[key] = iteratee(object[key], key, object)
})
return result
}
export default mapValue