0

I am trying to create a 2 dimensional array but am getting this error.

I loop an object and try to assign them but it won't let me assign a value to the second dimension.

This is what I have:

//this is globally set
var gcollision = new Array();

function create() {
    for (var X in sdata) {
        X = parseInt(X);
        for (var Y in sdata[X]) {
            Y = parseInt(Y);
            width = parseInt(sdata[X][Y][2]);
            height = parseInt(sdata[X][Y][3]);
            for (i = X; i != X + width; i++) {
                //error occurs here "Uncaught TypeError: Cannot set property '3' of undefined"
                gcollision[i][Y] = 1
                for (j = Y; j != Y + height; j++) {
                    gcollision[X][j] = 1
                }
            }
        }
    }

How do I make it set the value of properly?

EDIT sdata looks like this:

var sdata = {"4":{"7":["1","7","3","3"]},"3":{"3":["2","8","1","1"]}};
1
  • what does your sdata look like? Commented Apr 6, 2012 at 18:22

2 Answers 2

3
//this is globally set
var gcollision = new Array();

function create(){
    for (var X in sdata) {
        X = parseInt(X);
        for (var Y in sdata[X]) {
            Y = parseInt(Y);
            width = parseInt(sdata[X][Y][2]);
            height = parseInt(sdata[X][Y][3]);

for (i=X; i!= X+width; i++) {
                 if( typeof gcollision[i] == 'undefined' ) gcollision[i] = new Array();
                 gcollision[i][Y] = 1

for (j=Y; j!=Y+height; j++) {
                 if( typeof gcollision[X] == 'undefined' ) gcollision[X] = new Array();
                 gcollision[X][j] = 1
                }
            }
        }
    }

Basically, even though you created your array, those indices do not exist yet, so you cannot reference them as arrays until after you define them as such.

If you set up your for loops a bit more optimally, you don't have to do isset and can just create the gcol[index] = Array(); the right before the inner loop where it is first accessed.

Sign up to request clarification or add additional context in comments.

4 Comments

One final question, is making an array for this better than having objects ? Which would be more efficient for doing checks later on ?
Also i do not understand what you mean about the setup of loops more optimally?
In Javascript, you wont notice much of a difference between the two (arrays and objects). Internally, they're probably handled the same way anyway. As for the optimizations, we're currently checking gcollision[]'s type in the internal loop because I don't know what type of data you have in the width & height variables; if you could restructure your function such that the gcol[index] arrays are created outside of that loop, you would save yourself an order of if-operations.
There is a slight issue, see screenshot: i.imgur.com/eSBzi.jpg , i also added sdata format to the question at the bottom. Is it deffinately listed as you would have expected, im not sure i can see how X:Y link together to do checks?
1

You'll need to initialize the first level of arrays, first.

function create() {
    for (var X in sdata) {
        X = parseInt(X);
        gcollision[X] = [];

        for (var Y in sdata[X]) {
            Y = parseInt(Y);
            width = parseInt(sdata[X][Y][2]);
            height = parseInt(sdata[X][Y][3]);
            for (i = X; i < X + width; i++) {
                gcollision[i][Y] = 1;

                for (j = Y; j < Y + height; j++) {
                    gcollision[X][j] = 1;
                }
            }
        }
    }

2 Comments

Is this the most efficient way to do it ?
@Dave: Possibly. It shouldn't really matter that much, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.