TypeError: myArray[j] is undefined

Hi,

I have the following code in which I am trying to populate a two dimensional array (myArray) using the values from an object (myObject) but I am getting the error mentioned in the post title. The error is tied to the 4th line.

var myArray = [];
var j = 0;
for (var i in myObject) {
	myArray[j][0] = i;
	myArray[j][1] = myObject[i];
	myArray[j][2] = myObject[i] * 2;
	/* alert(myArray[j][0] + ', ' + myArray[j][1] + ', ' + myArray[j][2]) */
	j++;
}

When I test with alert(), only the first element of myArray is displayed, the rest seems not being defined and I don’t know what’s wrong.

Thanks for any ideas.

Doesn’t look like you’re defining a two-dimensional array.

I think

var myArray = [];

should be

var myArray = [[]];

Actually, I tried that one too before posting here, which gave the same error.

That the alert() displays the first element tells me that the code works only for the first iteration of j=0. Still can’t see what the issue is…

Forgetting my sample code for a minute, what I am trying to do is as follows:

I have a two dimensional array (array1). I want to create a new two dimensional array (array2) using the elements of the first array.

Ex:

array1 = [[name1, num1], [name2, num2], [name3, num3], ...]

array2 = [[name1, num1, func(num1)], [name2, num2, func(num2)], [name3, num3, func(num3)], ...]

How would I go about that?

Sorry for the multiple replies, but I believe if we figure out what is wrong in the following code, the issue will be solved:

var temp = [[]];
for (var i = 0; i < 5; i++) {
   	temp[i][0] = 'test';
   	alert(temp[i][0]);
}

The above alerts “test” only one time, whereas I would expect it to display for 5 times. And the Console gives “TypeError: temp[i] is undefined” error.

Should the third element in each array be the result of calling func() and passing it the previous element as an argument?

[quote=“James_Hibbard, post:7, topic:190412, full:true”]Should the third element in each array be the result of calling func() and passing it the previous element as an argument?
[/quote]

Hi, yes.

If you could please check my last reply before yours, you may notice what is wrong with my multidimensional array. Can’t see the issue myself.

This will log “test” to the console 5 times. Is that what you are trying to do?

var temp = [[]];
for (var i = 0; i < 5; i++) {
  temp[i] = ['test'];
  console.log(temp[i][0]);
}
1 Like

Seems putting ‘test’ between brackets did the trick, it is working as I intended now.

Still I don’t understand why this doesn’t work:

temp[i][0] = 'test';

but this does:

temp[i] = ['test'];

Thank you very much for your help.

I’ve never seen a multi-dimensional array declared like var temp = [[]];
I’d do something like

var temp = [];
for .....
 temp[i] = []; 
 temp[i][0] = "val";

I’d like to check it out, got a link to some documentation?

@Mittineague

Well, trying to collect bits of correct information on the web is sort of a tough task. I did a search for “javascript define multidimensional array” and some of the results suggested that declaration I had used. Even the first reply to this post did so.

No official documentation.

When you first start myArray = [] then myArray is an empty array. That means myArray[0] is empty (undefined), myArray[3] is undefined, myArray[10] is undefined, and so on. If you tried to access myArray[0][0], then you’re assuming myArray[0] is an array, but it’s not, it’s still undefined. myArray[0] = [i] works because instead of trying to access the 0-th element of a non-array, now you’re actually assigning the array.

That defines an array with one element that is an empty array.

The reason why temp[i][0] = 'test'; only works once is that only temp[0][0] exists. temp[1] isn’t an array and so has no [0] element.

For the loop to work you need an array in each of the first five positions in the array.

var temp = [[],[],[],[],[]];
for (var i = 0; i < 5; i++) {
   	temp[i][0] = 'test';
   	alert(temp[i][0]);
}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.