invert
object
Creates an object composed of the inverted keys and values of object.
Installation
Import
import { invert } from '@tulx/utils';Source Code
Implementation
/**
* Creates an object composed of the inverted keys and values of object.
*
* @param object - The object to invert.
* @returns Returns the new inverted object.
*
* @example
* ```ts
* const object = { 'a': 1, 'b': 2, 'c': 1 };
* invert(object); // { '1': 'c', '2': 'b' }
* ```
*/
export function invert(
object: Record<string, string | number>
): Record<string, string> {
const result: Record<string, string> = {};
for (const key in object) {
if (Object.prototype.hasOwnProperty.call(object, key)) {
const value = String(object[key]);
result[value] = key;
}
}
return result;
}
Example
import { invert } from '@tulx/utils';
const object = { 'a': 1, 'b': 2, 'c': 1 };
invert(object); // { '1': 'c', '2': 'b' }Related Functions
assign
Assigns own enumerable string keyed properties of source objects to the destination object.
assignIn
This method is like assign except that it iterates over own and inherited source properties.
assignInWith
This method is like assignIn except that it accepts customizer which is invoked to produce the assigned values.
assignWith
This method is like assign except that it accepts customizer which is invoked to produce the assigned values.
at
Creates an array of values corresponding to paths of object.
create
Creates an object that inherits from the prototype object.