I am try to set value get from String into 2D-Array.
But it does not work. error description in the picture.
My code:
data = "012021111"
function isGameOver(data){
var map = [[],[]];
var index = 0;
for (var i = 0; i < 3; i++) {
for (var j = 0; j < 3; j++) {
map[i][j] = data[index];
console.log("index: " +index+ ",i: " + i + ",j: " + j + ", data: " + map[i][j]);
index++;
}
}
}
error in your code is because you initialize map = [[], []]
that is map is an array containing two elements only which are again arrays. But you are trying to access map[2] which is undefined. A better solution would be to initialize map as an empty array and inside first for loop push arrays to map as required.
data = "012021111" function isGameOver(data){ var map = []; var index = 0; for (var i = 0; i < 3; i++) { map.push([]); for (var j = 0; j < 3; j++) { map[i][j] = data[index]; console.log("index: " +index+ ",i: " + i + ",j: " + j + ", data: " + map[i][j]); index++; } } } isGameOver(data);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.