0

I have a 2D JavaScript array;

grid = [ [ a, b],

         [ c, d]]

To access it's elements, i could use;

grid[ i ] // that's [ a, b] or [ c, d]

As an element of a sub-array, how do i address just "a" for instance?

Edit: The code is actually from a three.js scene where each element is represented by a cube i.e the array is a grid of cubes.

The program is set up such that when i click on one of these cubes, an event is triggered to display the name (string) of each of them. When i use

grid [ i ],

Each row returns the appropriate elements;

[ a, b ] or  [ c, d].

So to get the individual elements of each row, i figured it must be;

grid [ i ][ j ],

But when i ran the code, with this it always returned "undefined" so i thought the syntax was wrong....hence the original post.

Please what do i do to get a valid result?

Taiwofolu
  • 11
  • 3

3 Answers3

0

The simplest way

var grid = [ [ 1, 2], [ 3, 4]]
var a = grid[0][0] // 1
marsh
  • 1,431
  • 1
  • 13
  • 19
0

The Indexes of your multi-dimensional indexed array looks like this:

//grid = [ [ a, b], [ c, d]];
0: [  
  0: a,
  1: b
],
1: [
  0: c
  1: b
]

To access index of the grid array, you select them by calling grid[index] this returns the array at that given index. So when calling var row = grid[0], row will be an array with: [a, b].
This in turn can be accessed the same way: row[0] would be a.

To access without storing it in between, you would just use the index brackets twice:

var value = grid[0][0];
// value = a.
var value2 = grid[1][0];
// value = c.
Jite
  • 5,761
  • 2
  • 23
  • 37
  • I'm actually trying to get the index number/string value by clicking on the element ( it's a set of boxes). I think i grid [ i ][ j ] would've worked but it returns "undefined". I must be doing something wrong... – Taiwofolu Mar 14 '15 at 08:41
  • If you feel that the answer was not an answer to the question, your question is quite unclear. Furthermore, you accepted an answer that said pretty much the same thing as the one I wrote, and the one that @marsh wrote, but answered almost twenty minutes after the first answer was posted. If we did not answer your question correctly, how could the (now marked correctly) answer be correct? – Jite Mar 14 '15 at 10:05
  • It seems that your problem is not with javascripts array accessing, as the question seems to be about, but rather some issue with your GUI=>Code connection. Either edit your question, so that we can actually answer, knowing what the issue is, or accept an answer that answers the given question (which all three current answers does). – Jite Mar 14 '15 at 10:35
  • 1
    I'm sorry for the confusion, I've edited the question as you suggested. Also, i actually thought the 'tick' was a 'found this helpful' thing so i ticked all the answers. I've made amends. – Taiwofolu Mar 14 '15 at 11:31
0

The 2-d array grid = [ [ a, b], [ c, d]] could be accessed :

grid[0] = [a,b]; grid[0][0] = a;

If you want the array to be dumped :

for(var i=0;i<grid.length;i++){
   for(var j=0;j<grid[i].length;j++){
       console.log('grid[' + i + '][' + j + '] = ' + grid[i][j] );
   }
}

The output will be :

grid[0][0] = a

grid[0][1] = b

grid[1][0] = c

grid[1][1] = d

Abdessalem
  • 16
  • 1
  • I guess grid [ i ] [ j ] is what i was looking for. Unfortunately, for some reason, it always returns "undefined". – Taiwofolu Mar 14 '15 at 08:36