To map an object in JavaScript, you can use the Object.keys()
method to get an array of the object's keys. Then, you can use the Array.prototype.map()
method to iterate over the keys and perform operations on each key-value pair.
Here's an example:
index.tsx161 chars12 lines
In the above example, Object.keys(obj)
returns an array of keys: ['name', 'age', 'city']
. Then, the map()
method is called on this array and a new object is created for each key. Finally, the mapped object is logged to the console:
index.tsx56 chars2 lines
Note that the mapped object is an array of individual objects, where each object contains a single key-value pair from the original object.
gistlibby LogSnag