Issue
What's the easiest way to make nested objects in TypeScript? I want to be able to add elements in nested objects like this:
let dictionary: { [key: string]: { [key: string]: number } } = {};
dictionary['x']['y'] = 2
dictionary['x']['z'] = 5
I expect:
console.log(dictionary['x']) // -> {'y': 2,'z': 5}
console.log(dictionary['x']['y']) // -> 2
Solution
What's happening is that you are assigning the value 2
to the object at dictionary['x']
, which resolves to undefined
.
Try to initialise dictionary['x'] = {}
first.
Answered By - toanphan19
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.