1

I trying to set value in 3d javascript array...

var a = [];
var b = [];
var c = [];
var array = [a,b,c];
array[0][0][0] = 'value';

This code return error: "Cannot set property '0' of undefined" but 2d array working normally:

var a = [];
var b = [];
var array = [a,b];
array[0][0] = 'value';
2
  • Neither of those is a 3d array. They are both 2d arrays. Commented Nov 15, 2013 at 21:33
  • Don't you see that [a,b,c] is simply an array with three elements? Imagine it would be [1, 2, 3]. Commented Nov 15, 2013 at 21:52

2 Answers 2

2

That's not a 3D array, thats a 2D array, literally translated to:

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

Comments

2

in your first example you do not have a 3-dimensional array. what you have is [[],[],[]] - an array with 3 parallel arrays in it. this is why you can not access array[0][0][0] since the deepest you get is 2 levels. this is why your code in the second example works.

I think you got confused with the arrays at all. maybe you should read about programming, arrays and the whole idea again?


If you want to have an array with 3 leves, this is a nice example: an array in an array which is in an array itself

var a3l_a = [[[]]];
// equals:
var a3l_b = new Array(new Array(new Array()));

Comments

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.