Tarun S Sandhu
Tarun S Sandhu

Reputation: 51

Javascript array initialization behavior

// fixed a typo [] should be [[]] // sorry // but i remain stumped. // i want to initialize an empty array with the right number of dimensions and not right number of elements.

I am getting the following behavior.

var sdata = [[]];
sdata[0][0] = 4;
sdata[0][1] = 5;

it works.

but

var sdata = [[]];
sdata[0][0] = 4;
sdata[0][1] = 5;
sdata[1][0] = 6;

does not work , meaning the third element.

however

var sdata = [[]];
    sdata[0] = [];
    sdata[1] = [];
sdata[0][0] = 4;
sdata[0][1] = 5;
sdata[1][0] = 6;

also works.

Which means I cannot just create an empty array and give values to it. I have to count how many rows will it be, and then initialize an empty array of that much length and then give values to its elements. Which is particularly stupid because i don't know the length of data that I am going to get.

And when i say it does not work it means console.log prints vs does not print.

UPDATE : Yes this is stupid. But there is a silver line -- you only have to initialize at the first level. Levels below that become auto-initialized when you give them a value.

so basically, the following will work

var sdata = [[]];
sdata[0][0]= 4;  // first element initialization is not a problem
sdata[0][1]= 4;  // still falls under first

but following wont

var sdata = [[]];
sdata[0][0]= 4;
sdata[0][1]= 4;
sdata[1][0]= 4; // no it wont, anything after first parent element requires explicit initialization

so the following will

var sdata = [[]];
sdata[0]=[]
sdata[1]=[]     // explicit initialization
sdata[0][0]= 4;
sdata[0][1]= 4;
sdata[1][0]= 4; // now it will

however the following will work too

var sdata = [[]];
sdata[0]=[]
sdata[1]=[]
sdata[0][0]= 4;
sdata[0][1]= 4;
sdata[1][0]= 4;    // expected to work and will
sdata[1][1]= 4;    // expected to work and will
sdata[1][0][0]=5;  // expected to work since its first element and will
sdata[1][1][1]=5;  // expected not to work since its the second element , but it will work, since parent is initialized

bottom line - only first level initializations are required, levels below that is not a problem.

so now i am basically doing

for (x=0;x<y;x++)
        { sdata[x] = []; }  
sdata[c][d][e] = 4; // and so forth

where i gotta know y beforehand, and luckily i do.

Upvotes: 2

Views: 60

Answers (3)

CR Drost
CR Drost

Reputation: 9817

Which means I cannot just create an empty array and give values to it. I have to count how many rows will it be, and then initialize an empty array of that much length and then give values to its elements. Which is particularly stupid because i don't know the length of data that I am going to get.

Yes, it's stupid. It's stupid because computers are stupid. Deal with it. Lots of people have solved this problem before you, of course.

One way to solve it is to force the user to give you JSON. That's really easy. Now instead of trying to write

var sdata = [[]];
sdata[0][0] = 4;
sdata[0][1] = 5;
sdata[1][0] = 6;

you can just paste what they give you:

var sdata = [[4,5],[6]];

This is super-easy, and you can blame the user if things go wrong.

Another thing that you can do is to look for what sorts of data boundaries signify a new row. For example, when parsing CSV, you look for a newline character and increment the row index, and then you append a new array onto the output text.

Finally, if you insist on writing it out, just modify the original array! You can just as easily write the following:

var sdata = [];
sdata[0] = [];
sdata[0][0] = 4;
sdata[0][1] = 5;
sdata[1] = [];
sdata[1][0] = 6;

if you're into this whole "writing too much" shtick.

Upvotes: 5

Didier68
Didier68

Reputation: 1333

Multidimensional array doesn't really exists... Try this solution:

if ( sdata[ x ] === undefined) sdata[ x ] = [];
sdata[ x ] [ y ] = yourValue;

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477210

Your statements:

var sdata = [];
sdata[0][0] = 4;
sdata[0][1] = 5;

should fail, and running this with node (node.js), it does:

$ node
> var sdata = [];
undefined
> sdata[0][0] = 4;
TypeError: Cannot set property '0' of undefined
    at repl:1:13
    at REPLServer.defaultEval (repl.js:133:27)
    at bound (domain.js:254:14)
    at REPLServer.runBound [as eval] (domain.js:267:12)
    at REPLServer.<anonymous> (repl.js:280:12)
    at REPLServer.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:206:10)
    at REPLServer.Interface._line (readline.js:535:8)
    at REPLServer.Interface._ttyWrite (readline.js:812:14)
    at ReadStream.onkeypress (readline.js:108:10)

The reason is that sdata[0] returns undefined:

> sdata[0]
undefined

Now how can you add an item to undefined (which you can "compare" with a null pointer). You need at least an object to add items to.

You could solve the problem simply by generating some guard pattern:

function add_data (sdata,i,j,x) {
    var obj = sdata[i];
    if(obj === undefined) {
        obj = [];
        sdata[i] = obj;
    }
    obj[j] = x;
}

Which you could then call with:

var sdata = [];
add_data(sdata,0,0,4);
add_data(sdata,0,1,5);
add_data(sdata,1,0,5);

and will result in:

> sdata
[ [ 4, 5 ], [ 5 ] ]

Upvotes: 0

Related Questions