Gianmarco
Gianmarco

Reputation: 842

Nested hash map in javascript

I'm trying to create a nested hash map in js like in the example below:

let rooms = {};

rooms[roomNum][personName] = somethings

the problem is that when I try to do that I recive this error:

TypeError: Cannot set property 'personName' of undefined

I'm able to create just a "one level hash map" like that rooms[roomNum] = somethings

Upvotes: 0

Views: 688

Answers (1)

MauriceNino
MauriceNino

Reputation: 6757

  1. That's not a hashmap, it's an object
  2. rooms[roomNum] is not defined so you can't add something to rooms[roomNum][personName]

Here is an example on how you can do what you want to do:

let rooms = {};
rooms[roomNum] = {};
rooms[roomNum][personName] = somethings;

Upvotes: 2

Related Questions