Here's the TypeScript code that will take an input object, swap its keys with values and return the resulting object:
index.ts359 chars11 lines
With this function, you can pass any object as input and get the corresponding object with key-value pairs swapped. The function uses Object.keys()
to get the keys of the input object and then iterates over them using forEach()
. For each key-value pair, the function swaps the key with the value and adds it to the swappedObj
. Finally, the function returns the swappedObj
object.
Here's an example of how to use this function:
index.ts156 chars3 lines
In this example, the obj
object has its keys swapped with its values using the swapKeysWithValues
function. The resulting swappedObj
object has key-value pairs swapped and returned by the function.
gistlibby LogSnag