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;

Share Improve this question edited Oct 5, 2015 at 12:18 nowhere 1,5481 gold badge12 silver badges31 bronze badges asked Oct 5, 2015 at 11:33 user5353145user5353145 3
  • pathmap[upm_x] is undefined meaning pathmap array does not have element at the index of upm_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] IF pathmap[upm_x] is undefined. So make sure pathmap[upm_x] is defined as an array. – tiblu Commented Oct 5, 2015 at 11:38
Add a ment  | 

2 Answers 2

Reset to default 7

Despite 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