I'm running into trouble trying to access properties of a JSON object in my JavaScript code using obj[varWithPropName]. Strangely, it does work when I use obj["PropName"].
Here's a simplified snippet for reference:
import * as CharInfo from '../Configs/CharInfo.json';
(...)
this.ID = "Maya";
console.log("Maya" === this.ID); //true
console.log(typeof(CharInfo)); //object
console.log(CharInfo["Maya"]); //{configs: {…}, animations: {…}}
console.log(CharInfo[this.ID]); //undefined
The issue isn't limited to the console. In fact, when I try to access the property outside of console.log, it throws a runtime error:
Uncaught TypeError: Cannot read property 'animations' of undefined
Surprisingly, using the following syntax works:
CharInfo.default[this.ID]; //{configs: {…}, animations: {…}}
I'm curious to understand why this discrepancy exists rather than just finding a workaround. If anyone has encountered a similar problem before, I would greatly appreciate any insight! Thank you!