One way to do this is to use reduce()
function. Here's an example code:
index.ts251 chars8 lines
In this code, we create a function called convertObject
that receives an object with an id
and value
properties. Inside the function, we use object literal shorthand to create a new object with a key of obj.id
and a value of obj.value
. Finally, we return the new object.
Then, we create a sample object called data
with an id
of "4"
and value
of "tulum"
. We call the convertObject
function with this data
object and store the result in a variable called result
. Lastly, we log the result
variable to the console.
This implementation is very short and simple, but it assumes that the input object will always have an id
and value
property. If you need to handle different input shapes or handle missing properties, you'll need to modify this implementation.
gistlibby LogSnag