# Flatten a multi-dimensional Array

A commonly used array data structure is the multi-dimensional array, which is where an array is populated with arrays. One application of a multi-dimensional array is in representing a 2D game board of X x Y size. If duplication of this structure is necessary in application though, this becomes inefficient.

In order to overcome this, we can flatten the array. For instance, if we need a 2D array it could be constructed and populated like so:

```
var array = [];
for( var i = 0; i < width * height; i++){
board[i] = data;
}
```

Thus every 40th item in this array is the start of a new row in our 2D structure. Okay, simple. But how would we access data at X x Y coordinates?

```
array[ (x * width) + y ];
```

We can also do this with 3D arrays the setup would be the same with the added depth:

```
array[ width * height * depth];
```

And to access data located at x, y, z:

```
array[ x + width * (y + depth * z) ]
```