Creates an object with the same values as input object and keys generated by running each own enumerable property through an iteratee function
npx @tessl/cli install tessl/npm-lodash-mapkeys@3.8.0Lodash 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.
npm install lodashimport { mapKeys } from "lodash";For full lodash import:
import _ from "lodash";
// Use as _.mapKeys()CommonJS:
const { mapKeys } = require("lodash");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 }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
(value, key, object) => newKey - Custom transformation function_.property style)_.matches style predicate for transformation_.identity which converts values to string keysthisArg (*, optional): The this binding of iterateeReturns:
Object: Returns the new mapped object with transformed keysUsage 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' } } }Input Types:
{}Key Generation:
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
}The mapKeys function handles various edge cases gracefully:
{}undefined keys_.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