or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--mapvalues

The lodash method _.mapValues exported as a standalone Node.js module for creating objects with transformed values

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.mapvalues@4.6.x

To install, run

npx @tessl/cli install tessl/npm-lodash--mapvalues@4.6.0

index.mddocs/

Lodash MapValues

Lodash 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.

Package Information

  • Package Name: lodash.mapvalues
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.mapvalues

Core Imports

var mapValues = require('lodash.mapvalues');

For modern environments with ES modules:

import mapValues from 'lodash.mapvalues';

Basic Usage

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 }

Capabilities

Object Value Transformation

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);

Parameters

  • object (Object): The object to iterate over
  • iteratee (Function|Object|string) [optional]: The function invoked per iteration. Can be:
    • Function: Invoked with (value, key, object) arguments
    • String: Property name for shorthand property access (e.g., 'age')
    • Object: Partial object for partial matching
    • Undefined: Defaults to identity function (returns values unchanged)

Return Value

  • (Object): Returns the new mapped object with the same keys as the input object

Iteratee Function Signature

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);

Usage Examples

Function Iteratee

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' }

Property Name Shorthand

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' }

Object Iteratee (Partial Matching)

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 }

Complex Data Transformations

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' }
// }

Error Handling

The function handles edge cases gracefully:

  • Null/undefined objects: Returns empty object {}
  • Non-object inputs: Treats as objects where possible, may return empty object for primitives
  • Missing properties: Property shorthand returns undefined for missing properties
  • Iteratee errors: Errors thrown in iteratee functions will propagate
// 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 }

Performance Considerations

  • Property enumeration: Only processes own enumerable string-keyed properties
  • Iteration order: Not guaranteed to preserve property order in older JavaScript environments
  • Memory usage: Creates a new object; does not modify the original
  • Large objects: Performance scales linearly with the number of properties

Related Functionality

This package is part of the modularized lodash ecosystem. Related packages include:

  • lodash.mapkeys - Transform object keys instead of values
  • lodash.map - Transform array elements
  • lodash.foreach - Iterate without transformation
  • lodash - Full lodash library with all utilities