I want to add multiple keys to the dictionary via loop. For example, when I create a key, that key already has its value.
I have the following scenario:
var keys = ['key1', 'key2', 'key3', 'key4']
var dic = {}
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
dic[key] = 'value' + i
}
Note that I am adding several keys and already assigning the value of that key, but it does not work in TypeScript, so I have tested it works perfectly in JavaScript.
This line above has error. Note that I cannot add an element that is not contained in the array:
dic[key] = 'value' + i
Error details
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.
How can I get my Array keys and assign it to the dictionary with the loop value?