add after ary assign assignIn assignInWith assignWith at before bind bindAll bindKey camelCase capitalize castArray ceil chain chunk clamp clone cloneDeep cloneDeepWith cloneWith compact concat conformsTo countBy create curry curryRight debounce deburr defaults defaultsDeep defer delay difference divide drop dropRight each eachRight endsWith entries entriesIn eq escape every extend extendWith fill filter find findIndex findKey findLast findLastIndex findLastKey first flatMap flatMapDeep flatMapDepth flatten flattenDeep flattenDepth flip floor forEach forEachRight forIn forInRight forOwn forOwnRight fromPairs functions functionsIn get groupBy gt gte has hasIn head inRange includes indexOf initial intersection invert invertBy invoke invokeMap isArguments isArray isArrayBuffer isArrayLike isArrayLikeObject isBoolean isBuffer isDate isElement isEmpty isEqual isEqualWith isError isFinite isFunction isInteger isLength isMap isMatch isMatchWith isNaN isNative isNil isNull isNumber isObject isObjectLike isPlainObject isRegExp isSafeInteger isSet isString isSymbol isTypedArray isUndefined isWeakMap isWeakSet join kebabCase keyBy keys keysIn last lastIndexOf lowerCase lowerFirst lt lte map mapKeys mapValues max maxBy mean meanBy memoize merge mergeWith min minBy multiply negate nth omit omitBy once orderBy overArgs pad padEnd padStart parseInt partial partialRight partition pick pickBy pull pullAll pullAllBy pullAllWith pullAt random rearg reduce reduceRight reject remove repeat replace rest result reverse round sample sampleSize set setWith shuffle size slice snakeCase some sortBy sortedIndex sortedIndexOf sortedLastIndex sortedLastIndexOf sortedUniq split spread startCase startsWith subtract sum sumBy tail take takeRight tap template throttle thru toArray toFinite toInteger toLength toLower toNumber toPairs toPairsIn toPlainObject toSafeInteger toString toUpper transform trim trimEnd trimStart truncate unary unescape union uniq uniqBy uniqWith unset unzip unzipWith update updateWith upperCase upperFirst values valuesIn without words wrap wrapperAt wrapperChain wrapperCommit wrapperFlatMap wrapperLodash wrapperNext wrapperPlant wrapperReverse wrapperToIterator wrapperValue xor zip zipObject zipObjectDeep zipWith

Lodash _.invert


The lodash _.invert method is a powerful utility function that allows you to create a new object with inverted keys and values from the original object. This method is particularly useful when you need to reverse the order of an object's keys and values or determine unique values for a set of data.



The _.invert method takes an object parameter and returns a new object where the object's keys become the values and the object's values become the keys. If the original object has duplicate values, the method will override these values, and only one value will be returned in the inverted object. If you want to preserve all values, you can pass the _.invertBy method as the second argument.

Example usage:

const originalObject = { a: 1, b: 2, c: 2 };
const invertedObject = _.invert(originalObject);

// Output: { '1': 'a', '2': 'c' }

In this example, the _.invert method takes an object with keys a, b, c and values 1, 2, 2, respectively, and returns a new object where keys are 1 and 2 and their values are a and c, respectively.

When using _.invert, it is important to consider the type of key-value pairs in your original object. For example, if your original object has non-unique values, the resulting inverted object will conflict with certain key-value pairs. Additionally, if your original object contains values that are not strings or numbers, you will need to convert them to acceptable values before using _.invert.

Overall, the _.invert method can be a powerful tool for working with objects in JavaScript. With its simple syntax and ability to quickly invert keys and values, it is a useful utility function to keep in your development toolkit.