PDA

View Full Version : Resolved Multidimensional array

mrbean
Aug 13th, 2012, 06:52 AM
Why doesn't this work?

var candy = new Array(new Array());
for (var i = 0; i < 10; i++) {
candy[i][i] = "Sweet!";
}

The error that I get from chrome is:
Uncaught TypeError: Cannot set property '0' of undefined

I want to make an multidimensional array in an loop without knowing how long it can be.

minkoko
Aug 13th, 2012, 07:08 AM
var candy = new Array(new Array());
for ( i = 0; i < 10; i++) {
candy[i][i] = "Sweet!";
}

candy = new Array(3)
for (i = 0; i <candy.length; ++ i)
candy [i] = new Array(3);

mrbean
Aug 13th, 2012, 07:14 AM
var candy = new Array(new Array());
for ( i = 0; i < 10; i++) {
candy[i][i] = "Sweet!";
}

candy = new Array(3)
for (i = 0; i <candy.length; ++ i)
candy [i] = new Array(3);

"without knowing how long it can be"
So I don't want to guess how long it is like 3, or just make an extra big array to make sure it isn't to small. This will slowdown my app.

This is not where I am looking for sorry.

Old Pedant
Aug 13th, 2012, 07:31 AM
Doing this:

var candy = new Array(new Array());

creates your candy array with *ONE* element!

It is true that your ONE element is unbounded in size.

So you could code:

for ( var i = 0; i < 1000000; ++i )
{
candy[0][i] = "sweet";
}

But you could never put anything into candy[1][anyElement] because candy[1] is not an array.

You would need to do:

var candy = [ ];
for (var i = 0; i < 10; i++)
{
if ( candy[i] == null ) /* if no array in candy[i], must create it */
{
candy[i] = [ ];
}
candy[i][i] = "Sweet!";
}

Yes, you can use new Array( ) in place of [ ] if you really want to for some reason.

Old Pedant
Aug 13th, 2012, 07:37 AM
Something surprising to many JS newcomers is that you can do stuff like this:

var sweet = [ ];
sweet[0] = "This is a string";
sweet[1] = [ "this","is","an","array","of","strings"];
sweet[2] = 33;
sweet[3] = false;
sweet[4] = new Date();
sweet[999] = ["a", 3.14159265, "heterogeneous", document.createElement("hr"), "array" ];
sweet["and look here"] = sweet.length;

So the point is, JS doesn't know what each element of sweet[] *WILL BE* until you specify it. EACH element must be created separately.

mrbean
Aug 13th, 2012, 10:32 AM
But can you do something like this:

var sweet = [ ];
sweet[0][0] = "This is a string";
sweet[2][1] = 33;
sweet[3][3] = false;
sweet[4][5] = new Date();

sweet["and look here"] = sweet.length;
?

Philip M
Aug 13th, 2012, 10:57 AM
But can you do something like this:

var sweet = [ ];
sweet[0][0] = "This is a string";
sweet[2][1] = 33;
sweet[3][3] = false;
sweet[4][5] = new Date();

sweet["and look here"] = sweet.length;
?

No. You could have found that out simply by trying it.

But you could never put anything into candy[1][anyElement] because candy[1] is not an array.

Old Pedant
Aug 13th, 2012, 09:50 PM
If it isn't obvious by now, you would have to do

var sweet = [ ];
sweet[0] = [];
sweet[0][0] = "This is a string";
sweet[2] = [];
sweet[2][1] = 33;
sweet[3] = [];
sweet[3][3] = false;
sweet[4] = [];
sweet[4][5] = new Date();

sweet["and look here"] = sweet.length;

Or, as I showed in my first post, you could do

var sweet = [ ];
if ( sweet[0] == null ) sweet[0] = [];
sweet[0][0] = "This is a string";
if ( sweet[2] == null ) sweet[2] = [];
sweet[2][1] = 33;
if ( sweet[3] == null ) sweet[3] = [];
sweet[3][3] = false;
if ( sweet[4] == null ) sweet[4] = [];
sweet[4][5] = new Date();

sweet["and look here"] = sweet.length;

If sweet[3] already contains something, and you want to make sure it contains an array, you have to write

var sweet = [ ];
if ( ! Array.isArray(sweet[0]) ) sweet[0] = [];
sweet[0][0] = "This is a string";
if ( ! Array.isArray(sweet[2]) ) sweet[2] = [];
sweet[2][1] = 33;
if ( ! Array.isArray(sweet[3]) ) sweet[3] = [];
sweet[3][3] = false;
if ( ! Array.isArray(sweet[4]) ) sweet[4] = [];
sweet[4][5] = new Date();

sweet["and look here"] = sweet.length;

Old Pedant
Aug 13th, 2012, 09:52 PM
N.B.: Array.isArray() is not available in all browsers. There are work-arounds.

mrbean
Aug 14th, 2012, 02:13 AM
Finally fixed...

Solution:

var candys = new Array(candy_shop.length);
for(var i = 0; i < candy_shop.length; i++){
candy[i] = [];
}
candy[1][2] = "test";
console.log(candy[1][2]); // result: test

I think alot of people didn't know what i actually want, thats because of my english..
Sorry people!

Old Pedant
Aug 14th, 2012, 08:51 PM
No, not because of English.

Because you didn't show us THIS part:

var candy = new Array(candy_shop.length);

If you had shown us that in your first post, we would have know what you really needed.

But when you just start from

var candy = new Array(new Array());

as you did, that is something completely different. So we didn't guess what the actual problem was.

Your English, by the way, is *EXCELLENT*.

mrbean
Aug 15th, 2012, 02:40 AM
No, not because of English.

Because you didn't show us THIS part:

var candy = new Array(candy_shop.length);

If you had shown us that in your first post, we would have know what you really needed.

But when you just start from

var candy = new Array(new Array());

as you did, that is something completely different. So we didn't guess what the actual problem was.

Your English, by the way, is *EXCELLENT*.

New Array(new Array()) is where i started from, and then doing some research I found out that if I count the numbers of array values I needed that I can create Just enough for what I want.

And thanks for saying it's excelent :D!!