+ 9

Two dimensional array. How does it works?

I don't really understand

21st Oct 2017, 11:27 PM
HOHOUETO Ronald Boris
HOHOUETO Ronald Boris - avatar
6 Réponses
+ 11
@daemon you got it all right just a remark (not that I know better) indices in JavaScript starts at 0, so the element 3 would be arr[1][0] not arr[2][1]
21st Oct 2017, 11:50 PM
Hasan Fares
Hasan Fares - avatar
+ 9
they are mostly used to represent points on a graph (x and y coordinates), vectors, matrices,... .when you use them in an array you get to manipulate them easily. use double pair of square brackets to select the item inside the array of arrays: var a = [[1,2],[3,4]] alert(a[1][0]) outputs 3 since the 2nd array has index 1 in a, and in it 3 occupies index 0
21st Oct 2017, 11:42 PM
Hasan Fares
Hasan Fares - avatar
+ 8
Thanks ;-) I understand now
21st Oct 2017, 11:46 PM
HOHOUETO Ronald Boris
HOHOUETO Ronald Boris - avatar
+ 2
Well, it's an array, except every element in the array is another array. For instance, this: [[1, 1, 2], [3, 5, 8], [13, 21, 34]]. Let's say you wanted to access the element "3" in that array. In that case, if the array's name is arr, we'd get arr[2][1]. Also, you've seen a matrix, right? It helps to think of 2D arrays as matrices. The above would be: [ 1 1 2 ] [ 3 5 8 ] [ 13 21 34 ]
21st Oct 2017, 11:44 PM
DaemonThread
DaemonThread - avatar
+ 2
@Hasan Fares Oh, that's right! Thanks for catching my mistake. :D
22nd Oct 2017, 12:21 AM
DaemonThread
DaemonThread - avatar
+ 2
it is simply an array of arrays. to access a value, exampleArray[ ] [ ] the first [ ] being the parent array and the second [ ] the child.
22nd Oct 2017, 11:14 PM
jack