or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash-mapkeys

Creates an object with the same values as input object and keys generated by running each own enumerable property through an iteratee function

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

To install, run

npx @tessl/cli install tessl/npm-lodash-mapkeys@3.8.0

index.mddocs/

Lodash MapKeys

Lodash MapKeys provides the mapKeys function for creating objects with transformed keys while preserving the original values. It's the complement to mapValues, enabling key transformation through various iteratee types including functions, property paths, and custom transformations.

Package Information

  • Package Name: lodash
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash
  • Specific Function: mapKeys (from lodash utility library)

Core Imports

import { mapKeys } from "lodash";

For full lodash import:

import _ from "lodash";
// Use as _.mapKeys()

CommonJS:

const { mapKeys } = require("lodash");

Basic Usage

import { mapKeys } from "lodash";

// Transform keys using a function
const result = mapKeys({ a: 1, b: 2 }, function(value, key) {
  return key + value;
});
// => { 'a1': 1, 'b2': 2 }

// Transform keys using property path
const data = { user1: { name: 'alice' }, user2: { name: 'bob' } };
const byName = mapKeys(data, 'name');
// => { 'alice': { name: 'alice' }, 'bob': { name: 'bob' } }

// Use with thisArg for context binding
const context = { prefix: 'key_' };
const prefixed = mapKeys({ a: 1, b: 2 }, function(value, key) {
  return this.prefix + key;
}, context);
// => { 'key_a': 1, 'key_b': 2 }

Capabilities

Object Key Transformation

Creates an object with the same values as the input object and keys generated by running each own enumerable property through an iteratee function.

/**
 * Creates an object with the same values as `object` and keys generated by 
 * running each own enumerable property of `object` through `iteratee`.
 * 
 * @param {Object} object - The object to iterate over
 * @param {Function|Object|string} [iteratee=_.identity] - The function invoked per iteration
 * @param {*} [thisArg] - The `this` binding of `iteratee`
 * @returns {Object} Returns the new mapped object
 */
function mapKeys(object, iteratee, thisArg);

Parameters:

  • object (Object): The object to iterate over. Can also be an array (treated as object with index keys)
  • iteratee (Function|Object|string, optional): The function invoked per iteration. Defaults to _.identity
    • Function: (value, key, object) => newKey - Custom transformation function
    • String: Property path to extract nested value as key (uses _.property style)
    • Object: Uses _.matches style predicate for transformation
    • Omitted: Uses _.identity which converts values to string keys
  • thisArg (*, optional): The this binding of iteratee

Returns:

  • Object: Returns the new mapped object with transformed keys

Usage Examples:

import { mapKeys } from "lodash";

// Function iteratee - custom key transformation
const users = { user1: 'Alice', user2: 'Bob' };
const result1 = mapKeys(users, (value, key) => `${key}_${value.toLowerCase()}`);
// => { 'user1_alice': 'Alice', 'user2_bob': 'Bob' }

// String iteratee - property path extraction
const data = {
  item1: { id: 'x', name: 'First' },
  item2: { id: 'y', name: 'Second' }
};
const byId = mapKeys(data, 'id');
// => { 'x': { id: 'x', name: 'First' }, 'y': { id: 'y', name: 'Second' } }

// Array input (treated as object with index keys)
const array = ['foo', 'bar'];
const arrayResult = mapKeys(array, (value, index) => `item_${index}`);
// => { 'item_0': 'foo', 'item_1': 'bar' }

// Context binding with thisArg
const transformer = {
  suffix: '_transformed',
  transform: function(value, key) {
    return key + this.suffix;
  }
};
const contextResult = mapKeys(
  { a: 1, b: 2 }, 
  transformer.transform, 
  transformer
);
// => { 'a_transformed': 1, 'b_transformed': 2 }

// No iteratee - uses identity (converts values to keys)
const identityResult = mapKeys({ a: 1, b: 2 });
// => { '1': 1, '2': 2 }

// Nested property access
const complexData = {
  user1: { profile: { username: 'alice123' } },
  user2: { profile: { username: 'bob456' } }
};
const byUsername = mapKeys(complexData, 'profile.username');
// => { 'alice123': { profile: { username: 'alice123' } }, 'bob456': { profile: { username: 'bob456' } } }

Type Behavior

Input Types:

  • Object: Standard object with enumerable properties
  • Array: Treated as object with numeric index keys (0, 1, 2, etc.)
  • Null/Undefined: Returns empty object {}

Key Generation:

  • Keys are always converted to strings (JavaScript object key behavior)
  • Duplicate generated keys will overwrite previous entries
  • Non-enumerable properties are ignored
  • Only own properties are processed (not inherited)

Iteratee Function Signature:

function iteratee(value, key, object) {
  // value: the property value
  // key: the property key (string or number for arrays)
  // object: the original object being iterated
  // return: the new key for this property
}

Error Handling

The mapKeys function handles various edge cases gracefully:

  • Invalid iteratee: Falls back to identity transformation
  • Non-objects: Empty or falsy inputs return empty object {}
  • Circular references: Processes enumerable properties without infinite loops
  • Property access errors: String iteratees that reference non-existent paths return undefined keys

Related Functions

  • _.mapValues - Maps values instead of keys (companion function)
  • _.transform - More flexible object transformation utility
  • _.keyBy - Creates object with keys from iteratee results (similar use case)
  • _.invert - Swaps keys and values of an object