I have a object that looks like this:
obj: {
centricAccountId1: number;
centricAccountId2: number;
...
centricAccountId10: number;
}
I'm writing a for loop to assign values to each key:
for (let i=1; i < 11 ;i++) {
obj.centricAccountId${i} = //something
}
documentItem.centricAccountId${i} doesn't seem to work. how should I iterate through these keys without repeating their names ?
You were close, but the code was wrong. You'll need to access the keys using square brackets and use template literals. You can try something like this
const obj = {
centricAccountId1: 123,
centricAccountId2: 234,
centricAccountId3: 891
}
for (let i=1; i <= 3 ;i++) {
obj[`centricAccountId${i}`] = obj[`centricAccountId${i}`]+1
}
console.log(obj)