0

I know that this question has been asked over and over. There are extensive discussions here and here, and a ton of other places online, but I just can't seem to work it out.

I want to store the contents of a gridview in a 2D array in Javascript. I have 4 columns and a varying number (100 to 300) of rows.

I've tried a number of different approaches to create and fill this array. They all take the basic form:

myVar = [ [] ];
myVar = [ [], [], [], [] ];
myVar = new Array([]);
myVar = new Array(new Array());

These are just a few of the approaches I've found and tried, but they either don't work or they only work on rows 0 and 1 (several of the proposed solutions have associated comments that mention this, so I know I'm not imagining it).

Many suggested solutions use loops, and some even use functions, but I can't imagine that this simple task, to create a 2D array, needs to be that complicated.

Can anyone please post simple syntax that creates a 2D array in Javascript, and the syntax to fill it and to access it? Many thanks.

buckshot
  • 315
  • 4
  • 15
  • You show only the create, but not the access. what/how did you try to access it? The creation is correct... – Dekel Aug 10 '17 at 21:42
  • Please provide a [minimal, verifiable, and complete](https://stackoverflow.com/help/mcve) example. – McHat Aug 10 '17 at 21:46
  • 1
    In JS arrays are reference type and creating an ND array is not trivial. Have a look at [this](https://stackoverflow.com/a/44965260/4543207) – Redu Aug 10 '17 at 21:46
  • Well, a few things. For example, myVar[0][0][0][0] = 'hello', which is a syntax error. That's part of my question, it isn't clear from the pages I've visited how to fill and access the array. – buckshot Aug 10 '17 at 21:47
  • 1
    It is clear you have much to learn. Walk yourself through this and come back if you still have a problem: https://javascript.info/array – McHat Aug 10 '17 at 21:49
  • @Redu, thanks for that link. I guess this isn't the straightforward problem I imagined. Other languages make this simple, so I was assuming JS would be the same. – buckshot Aug 10 '17 at 21:54
  • 1
    what about this? new Array(100).fill( new Array(4).fill(0),0,99) – Daniel Miron Aug 10 '17 at 21:54
  • Well... sorry to say but since in JS all objects are reference types and since arrays are in fact just objects that's just how it is if you want to initialize an indefinite dimension array (including 2) – Redu Aug 10 '17 at 21:57
  • @Redu, back to the drawing board, I guess... thanks again. – buckshot Aug 10 '17 at 21:58
  • 1
    @buckshot `myVar[0][0][0][0]` would imply that your Array has at least 4 dimensions, not just the 2 that you initialized in your example. `myVar[firstAxis][secondAxis][thirdAxis][fourthAxis]` and so on. – Thomas Aug 10 '17 at 21:59
  • @Daniel Miron, I'm tinkering with your solution, but I may need explore other options. Thanks for your reply, tho'. – buckshot Aug 10 '17 at 22:00
  • @Thomas, you're right, I blew that one. My actual syntax is var allArray =[ [], [] ], to fill is allArray[0] [0] = 'hello';, to access is otherVar = allArray[0][0]; Works fine on rows 0 and 1, but crashes after that. – buckshot Aug 10 '17 at 22:04

2 Answers2

0

Full disclosure: I'm still relatively new to Javascript myself, so I'm not sure if this an idiomatic or even all that wise of a solution, but you could try something like:

var defaultValue = 0;
var arr2D = new Array(300).fill( new Array(4).fill(defaultValue) );
0

You can create a 2D array in javascript like so

twoDArray = [ [1, 2, 3, 4], [5, 6, 7, 8] ];
alert(twoDArray[0][1]); // results in 2

You can access it by first providing the array location "0" and then the element location "1".

You can iterate over it like so

for(var x = 0; x < twoDArray.length; x++)
{
    for(var y = 0; y < twoDArray[x].length; y++)
  {
    alert(twoDArray[x][y]);
  }
}

jsFiddle: https://jsfiddle.net/81j5uug7/

Canvas
  • 5,779
  • 9
  • 55
  • 98
  • Thanks. A lot of the replies have used this approach, but I can't just supply my values to my array by putting them in brackets when I declare the array. I need something with hundreds of rows that I can reuse. – buckshot Aug 10 '17 at 22:30