Noob question, I Can't seem to find a way to interate an object with dynamic keys in typescript
//This is how i type my object
let obj : { [ key : string ], string } = {};
and using forEach or map doesn't work and giving me errors. Pls help
Noob question, I Can't seem to find a way to interate an object with dynamic keys in typescript
//This is how i type my object
let obj : { [ key : string ], string } = {};
and using forEach or map doesn't work and giving me errors. Pls help
Try:
for (const key in obj) {
// Logic here
}
"key" will store each individual key for every iteration of the loop, and you can use obj[key] to get the value.
Also, if you're making an object with key-value pairs of type string and string, use
let obj: Record<string, string> = {};
Record is a good type to use for string-string objects.
It would be helpful if you posted exactly what error you were encountering!