The lodash method _.mapValues exported as a standalone Node.js module for creating objects with transformed values
npx @tessl/cli install tessl/npm-lodash--mapvalues@4.6.0Lodash MapValues provides the lodash _.mapValues method as a standalone Node.js module. This utility function enables developers to create new objects with the same keys as the original object but with values generated by running each property value through an iteratee function. The function supports various iteratee formats including functions, property names, and partial objects, making it highly versatile for data transformation tasks.
npm install lodash.mapvaluesvar mapValues = require('lodash.mapvalues');For modern environments with ES modules:
import mapValues from 'lodash.mapvalues';var mapValues = require('lodash.mapvalues');
// Transform object values with a function
var users = {
'fred': { 'user': 'fred', 'age': 40 },
'pebbles': { 'user': 'pebbles', 'age': 1 }
};
var ages = mapValues(users, function(o) {
return o.age;
});
// => { 'fred': 40, 'pebbles': 1 }
// Use property name shorthand
var agesByProperty = mapValues(users, 'age');
// => { 'fred': 40, 'pebbles': 1 }Creates an object with the same keys as the input object and values generated by running each own enumerable string keyed property through an iteratee function.
/**
* 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).
*
* @param {Object} object The object to iterate over.
* @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration.
* @returns {Object} Returns the new mapped object.
*/
function mapValues(object, iteratee);object (Object): The object to iterate overiteratee (Function|Object|string) [optional]: The function invoked per iteration. Can be:
(value, key, object) arguments'age')When using a function as the iteratee:
/**
* @param {*} value The current property value
* @param {string} key The current property key
* @param {Object} object The source object being iterated
* @returns {*} The transformed value for this property
*/
function iteratee(value, key, object);Transform values using a custom function:
var mapValues = require('lodash.mapvalues');
var users = {
'alice': { 'name': 'Alice', 'age': 25, 'active': true },
'bob': { 'name': 'Bob', 'age': 30, 'active': false }
};
// Extract and transform specific properties
var userSummaries = mapValues(users, function(user, key) {
return user.name + ' (' + user.age + ' years old)';
});
// => { 'alice': 'Alice (25 years old)', 'bob': 'Bob (30 years old)' }
// Calculate derived values
var userStatuses = mapValues(users, function(user) {
return user.active ? 'active' : 'inactive';
});
// => { 'alice': 'active', 'bob': 'inactive' }Use property name strings for simple property extraction:
var products = {
'laptop': { 'name': 'Laptop', 'price': 999, 'category': 'electronics' },
'book': { 'name': 'Book', 'price': 29, 'category': 'education' }
};
// Extract prices using property shorthand
var prices = mapValues(products, 'price');
// => { 'laptop': 999, 'book': 29 }
// Extract categories
var categories = mapValues(products, 'category');
// => { 'laptop': 'electronics', 'book': 'education' }Use objects for partial matching patterns:
var items = {
'item1': { 'type': 'book', 'genre': 'fiction', 'available': true },
'item2': { 'type': 'book', 'genre': 'non-fiction', 'available': false },
'item3': { 'type': 'dvd', 'genre': 'action', 'available': true }
};
// Check if items match specific criteria
var fictionBooks = mapValues(items, { 'type': 'book', 'genre': 'fiction' });
// => { 'item1': true, 'item2': false, 'item3': false }var inventory = {
'warehouse-a': {
'items': ['laptop', 'mouse', 'keyboard'],
'capacity': 1000,
'location': 'Seattle'
},
'warehouse-b': {
'items': ['monitor', 'cable', 'speakers'],
'capacity': 500,
'location': 'Portland'
}
};
// Transform to summary format
var warehouseSummaries = mapValues(inventory, function(warehouse, key) {
return {
'id': key,
'itemCount': warehouse.items.length,
'utilization': (warehouse.items.length / warehouse.capacity * 100).toFixed(1) + '%',
'location': warehouse.location
};
});
// => {
// 'warehouse-a': { 'id': 'warehouse-a', 'itemCount': 3, 'utilization': '0.3%', 'location': 'Seattle' },
// 'warehouse-b': { 'id': 'warehouse-b', 'itemCount': 3, 'utilization': '0.6%', 'location': 'Portland' }
// }The function handles edge cases gracefully:
{}undefined for missing properties// Safe handling of null/undefined
mapValues(null, 'someProperty'); // => {}
mapValues(undefined, function(v) { return v; }); // => {}
// Handles missing properties gracefully
mapValues({ a: { x: 1 }, b: { y: 2 } }, 'x');
// => { a: 1, b: undefined }This package is part of the modularized lodash ecosystem. Related packages include:
lodash.mapkeys - Transform object keys instead of valueslodash.map - Transform array elementslodash.foreach - Iterate without transformationlodash - Full lodash library with all utilities