The lodash methods invert and invertBy exported as a module for creating objects with inverted keys and values.
npx @tessl/cli install tessl/npm-lodash--invert@4.3.0Lodash Invert provides the lodash invert and invertBy methods as standalone, modular utilities for creating objects composed of inverted keys and values. It includes both basic inversion (invert) and advanced grouping inversion (invertBy) functionality. This is a lightweight alternative to importing the entire lodash library when only inversion capabilities are needed.
npm install lodash.invertvar invert = require('lodash.invert');
var invertBy = require('lodash.invert').invertBy;For ES6/ESM environments:
import invert, { invertBy } from 'lodash.invert';var invert = require('lodash.invert');
var invertBy = require('lodash.invert').invertBy;
// Basic inversion
var object = { 'a': 1, 'b': 2, 'c': 1 };
var result = invert(object);
console.log(result);
// => { '1': 'c', '2': 'b' }
// Inversion with grouping
var resultBy = invertBy(object);
console.log(resultBy);
// => { '1': ['a', 'c'], '2': ['b'] }
// Real-world example: Creating lookup tables
var statusCodes = { 'ok': 200, 'error': 500, 'notFound': 404 };
var codeToStatus = invert(statusCodes);
console.log(codeToStatus);
// => { '200': 'ok', '500': 'error', '404': 'notFound' }Creates an object composed of the inverted keys and values of the input object. When duplicate values exist in the original object, subsequent values overwrite property assignments of previous values.
/**
* Creates an object composed of the inverted keys and values of object.
* If object contains duplicate values, subsequent values overwrite property
* assignments of previous values.
*
* @param {Object} object The object to invert.
* @returns {Object} Returns the new inverted object.
*/
function invert(object);Creates an object composed of inverted keys and values, but uses an iteratee function to transform values before inversion. The inverted value of each inverted key is an array of keys responsible for generating that inverted value.
/**
* This method is like invert except that the inverted object is generated
* from the results of running each element of object through iteratee.
* The corresponding inverted value of each inverted key is an array of keys
* responsible for generating the inverted value.
*
* @param {Object} object The object to invert.
* @param {Function|Object|string} [iteratee=_.identity] The iteratee invoked per element.
* @returns {Object} Returns the new inverted object.
*/
function invertBy(object, iteratee);Parameters for invert:
object (Object): The object to invertReturns:
Parameters for invertBy:
object (Object): The object to invert[iteratee=_.identity] (Function|Object|string): The iteratee invoked per elementReturns:
Behavior:
Usage Examples:
// Basic inversion
var colors = { red: '#ff0000', blue: '#0000ff', green: '#00ff00' };
invert(colors);
// => { '#ff0000': 'red', '#0000ff': 'blue', '#00ff00': 'green' }
// Duplicate values - last key wins
var ratings = { good: 5, excellent: 5, poor: 1, bad: 1 };
invert(ratings);
// => { '5': 'excellent', '1': 'bad' }
// invertBy with duplicate values - keys grouped in arrays
invertBy(ratings);
// => { '5': ['good', 'excellent'], '1': ['poor', 'bad'] }
// invertBy with iteratee function
var scores = { alice: 95, bob: 87, charlie: 95, dave: 87 };
invertBy(scores, function(value) {
return 'grade-' + (value >= 90 ? 'A' : 'B');
});
// => { 'grade-A': ['alice', 'charlie'], 'grade-B': ['bob', 'dave'] }
// Mixed value types
var mixed = { a: 1, b: true, c: 'hello' };
invert(mixed);
// => { '1': 'a', 'true': 'b', 'hello': 'c' }
// Empty object
invert({});
// => {}
// Common use case: Creating reverse lookup maps
var userRoles = { admin: 1, editor: 2, viewer: 3 };
var roleIds = invert(userRoles);
// Use roleIds['1'] to get 'admin', etc.