admin管理员组文章数量:1335179
I know this has been asked a lot of times, but how do I fix exactly this thing?
I have a map[][] array (contains tile ids for a game) and I need to copy it to pathmap[][] array (contains just 0's and 1's, it is a path map), however when I do so..
function updatepathmap(){
pathmap = [];
var upm_x = 0;
while (upm_x < map.length){
var upm_y = 0;
while (upm_y < map[upm_x].length){
pathmap[][]
if (canPassthrough(map[upm_x][upm_y])) {
pathmap[upm_x][upm_y] = 1;
} else {
console.log(upm_x);
console.log(upm_y);
pathmap[upm_x][upm_y] = 0;
}
upm_y++;
}
upm_x++;
}
console.log(map);
console.log(pathmap);
}
..it gives me Cannot set property '0' of undefined
typeerror at line pathmap[upm_x][upm_y] = 0;
I know this has been asked a lot of times, but how do I fix exactly this thing?
I have a map[][] array (contains tile ids for a game) and I need to copy it to pathmap[][] array (contains just 0's and 1's, it is a path map), however when I do so..
function updatepathmap(){
pathmap = [];
var upm_x = 0;
while (upm_x < map.length){
var upm_y = 0;
while (upm_y < map[upm_x].length){
pathmap[][]
if (canPassthrough(map[upm_x][upm_y])) {
pathmap[upm_x][upm_y] = 1;
} else {
console.log(upm_x);
console.log(upm_y);
pathmap[upm_x][upm_y] = 0;
}
upm_y++;
}
upm_x++;
}
console.log(map);
console.log(pathmap);
}
..it gives me Cannot set property '0' of undefined
typeerror at line pathmap[upm_x][upm_y] = 0;
-
pathmap[upm_x]
is undefined meaningpathmap
array does not have element at the index ofupm_x
.console.log(pathmap[upm_x])
to check. – tiblu Commented Oct 5, 2015 at 11:36 -
@tiblu but I am defining it in
pathmap[upm_x][upm_y] = 0;
– user5353145 Commented Oct 5, 2015 at 11:37 -
1
Yes, but you cannot define
pathmap[upm_x][upm_y]
IFpathmap[upm_x]
is undefined. So make surepathmap[upm_x]
is defined as an array. – tiblu Commented Oct 5, 2015 at 11:38
2 Answers
Reset to default 7Despite the foo[0][0]
syntactic sugar, multi-dimensional arrays do not really exist. You merely have arrays inside other arrays. One consequence is that you cannot build the array in the same expression:
> var foo = [];
undefined
> foo[0][0] = true;
TypeError: Cannot set property '0' of undefined
You need to create parent array first:
> var foo = [];
undefined
> foo[0] = [];
[]
> foo[0][0] = true;
true
You can determine whether it exists with the usual techniques, e.g.:
> var foo = [];
undefined
> typeof foo[0]==="undefined"
true
> foo[0] = true;
true
> typeof foo[0]==="undefined"
false
I would have thought pathmap[][]
was a syntax error, I'm surprised you're not seeing one.
Before you can use an array at pathmap[upm_x]
, you must create an array at pathmap[upm_x]
:
pathmap[upm_x] = [];
This would be the first line in your outer while
, so:
while (upm_x < map.length){
pathmap[upm_x] = [];
// ...
Remember that JavaScript doesn't have 2D arrays. It has arrays of arrays. pathmap = []
creates the outer array, but doesn't do anything to create arrays inside it.
Side note:
var upm_x = 0;
while (upm_x < map.length){
// ...
upm_x++;
}
is an error-prone way to write:
for (var upm_x = 0; upm_x < map.length; upm_x++){
// ...
}
If you use while
, and you have any reason to use continue
or you have multiple if
branches, it's really easy to forget to update your looping variable. Since looping on a control variable is what for
is for, it's best to use the right construct for the job.
Side note 2:
Your code is falling prey to The Horror of Implicit Globals because you don't declare pathmap
. Maybe you're doing that on purpose, but I wouldn't remend it. Declare your variable, and if you need it outside your function, have your function return it.
Side note 3:
map
would make this code a lot simpler:
function updatepathmap(){
var pathmap = map.map(function(outerEntry) {
return outerEntry.map(function(innerEntry) {
return canPassthrough(innerEntry) ? 1 : 0;
});
});
console.log(map);
console.log(pathmap);
}
本文标签: javascriptCannot set property 39039 of undefined in 2d arrayStack Overflow
版权声明:本文标题:javascript - Cannot set property '0' of undefined in 2d array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742382623a2464395.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论