-2

As explained in the title I'm having a hard time trying to make a thing that seems simple work. Here's a simple example, it's easier to understand.

var reccords = {
  1: {
    1: [10, 'zorro'],
    2: [25, 'ro']
  },
  2: {
    1: [20, "dim"]
  }
}

var time = 5;
reccords[3] = {
  time: [2, 'michel']
}; //time isn't considered as a variable

document.getElementById("demo2").innerHTML = reccords[3]['time'][0];
document.getElementById("demo1").innerHTML = reccords[3][5][0]; //does not work, how to make it work?
<p id="demo1"></p>
<p id="demo2"></p>
mplungjan
  • 169,008
  • 28
  • 173
  • 236

1 Answers1

1

Try following use [time] to set value of time i.e. 5 as the property of object. With this, in case of reccords[3]['time'][0], you need to use reccords[3][time][0] as records[3] does not have time property.

var reccords = {1:{1:[10,'zorro'],2:[25,'ro']}, 2:{1:[20,"dim"]}}

var time=5;
reccords[3] = {[time]:[2, 'michel']}; //time isn't considered as a variable

document.getElementById("demo2").innerHTML = reccords[3][time][0];
document.getElementById("demo1").innerHTML = reccords[3][5][0];
<p id="demo1"></p>
<p id="demo2"></p>
Nikhil Aggarwal
  • 28,197
  • 4
  • 43
  • 59