0

After hours of research, I cannot figure out how to make a 161x101 array in JavaScript. I feel like this should be JavaScript 101, but I can't figure it out.

This is what happen when I try the most logical thing to do.

<head>
<script>
var StopTimes = [161,101];
StopTimes[1][1] = "TEST";
StopTimes[33][64] = "TEST2";
console.log(StopTimes[1][1]);
console.log(StopTimes[33][64]);
console.log(StopTimes[15][3]);
</script>
</head>

Error Message:

enter image description here

I've spent hours trying other things, but nothing works. Am I just stupid? What do I do to make a 161x101 array?

Paul T.
  • 4,703
  • 11
  • 25
  • 29
  • 3
    `StopTimes` is an array containing two items, numbers. `StopTimes[1]` refers to a number. Assigning to the `[1]` property of a number doesn't make any sense – CertainPerformance Nov 06 '20 at 04:28
  • And to create the array of `n` size https://stackoverflow.com/a/966938/600486 – blurfus Nov 06 '20 at 04:38
  • See https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – Notaprobuthearmeout Nov 06 '20 at 04:44
  • //this would create the two-dimensional array of the size you specified. `StopTimes = Array.from(Array(161),() => new Array(101)); ` //assign what ever value you want for specified index. `StopTimes[1][1] = "TEST"; StopTimes[33][64] = "TEST2";` – Bhanu Prakash War Nov 06 '20 at 06:25

2 Answers2

0

can easily use

 var StopTimes=new Array(161);
 
 StopTimes.foreach(function(item,index){StopTimes[index]=new Array(101)  });
0
var StopTimes = [];
for (var i = 0; i< 161; i++){
  var tmp = [];
  for(var j=0; j<101; j++){
     tmp.push(0);//or any data element
   }
  StopTimes.push(tmp);
}
Hani
  • 149
  • 1
  • 3