I have a string that has been converted into an 2D Array in js.
It is used to represent a game board
each . represents a space
each | represents a row
each X represents a wall
var src= "...|.X.|...|";
board = src.split(/\|/g);
for (var i = 0; i < board.length; i++) {
var cells = board[i].split('');
for (var j = 0; j < cells.length; j++) {
cells[j] = parseInt(cells[j]);
}
board[i][j] = cells;
console.log(board[1][1])
//returns 'X'
when i access board[i][j] it returns correctly:
etc etc
I want to update the specific element with a string representing a piece. However when i insert into an element like so:
The array returns in firebug as so:
When it should look like:
Why are elements [0][1] and [0][2] being overwritten? Am I not understanding arrays of array index access correctly in js?
In Java, elements in a 2D array can be modified in a similar fashion to modifying elements in a 1D array. Setting arr[i][j] equal to a new value will modify the element in row i column j of the array arr .
To update all the elements of an array, call the forEach() method on the array, passing it a function. The function gets called for each element in the array and allows us to update the array's values.
No, we cannot store multiple datatype in an Array, we can store similar datatype only in an Array.
Javascript has no inbuilt support for multidimensional arrays, however the language is flexible enough that you can emulate this behaviour easily by populating your arrays with separate arrays, creating a multi-level structure.
I just had the same problem, but it had a more complex reason and I want to add it, in case someone finds this page searching for the same problem I had:
I had created and filled a 2-dimensional array like this:
var foo = Array(n).fill(Array(n).fill(0));
which creates a 2-dimensional n*n array filled with zeroes.
Now when I tried to overwrite a cell like this
foo[1][1] = 1;
I ended up with these values:
[[0,1,0],
[0,1,0],
[0,1,0]]
which is really surprising IMHO.
The reason for this was, that there has only been one row, which had internally been referenced three times. So when I changed the first index in "the second" row, it effectively changed all rows.
Bottom line: don't use Array.fill
to create multi-dimensional arrays!
I'm betting that you have a one-dimensional array with strings stored in each. So your array actually looks like:
array (
[0] => '...',
[1] => '.X.',
[2] => '...'
)
When this is what you want:
array (
[0] => array (
[0] => '.',
[1] => '.',
[2] => '.'
),
[1] => array (
[0] => '.',
[1] => 'X',
[2] => '.'
),
[2] => array (
[0] => '.',
[1] => '.',
[2] => '.'
)
)
When constructing your 2D array, make sure you explicitly declare each entry in board
as an array. So to construct it, your code might look something like this:
board = new Array();
rows = 3;
for (var i = 0; i < rows; i++)
board[i] = new Array('.', '.', '.');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With