3
var a = new Array();
var b  = [1,2,3,4,5,6,7];
for(i in b){a[i] = new Array(new Array());}
a[0][1][2] = Math.random();// error:VM205:1 Uncaught TypeError: Cannot set property '2' of undefined(…)

How to use three-dimensional array in javascript?

i mean how to use it like java.
eg: double a = new double[length][length][length];

how to memorize memory in advance.

3
  • 1
    You need to initialize each array with the values you want. There's no true multi-dimensional array that will be fully allocated with merely a declaration. Commented Nov 1, 2016 at 12:03
  • Its because, new Array will set an array of length 0. So a[0][0] will have an array, but a[0][1] will be undefined. If you try to explain what you are trying to achieve, we could recommend something Commented Nov 1, 2016 at 12:04
  • 1
    Possible duplicate: stackoverflow.com/questions/7545641/… Commented Nov 1, 2016 at 12:05

4 Answers 4

3

In Javascript, there's no use in instantiating variables in advance, and there is no such thing as compile-time memory allocation because, hey, there's no compile time! But if you really want to do it, it's not as trivial as in Java:

const length = 7;
const range = new Array(length).fill();
const array = range.map(e => range.map(e => range.map(e => e)));

console.log(JSON.stringify(array)); // -> "[[[null,null,null],[null,null,null],[null,null,null]],[[null,null,null],[null,null,null],[null,null,null]],[[null,null,null],[null,null,null],[null,null,null]]]"

The only point in it is that you can be always sure that, as long as you stay in [0, length) boundaries, array[x][y][z] for any x, y and z will not throw a TypeError.

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

Comments

1

you can do like this

const items = [[[]]] // init 3d array

// assign values
items[0][0][0] = 0
items[0][0][1] = 1
items[0][0][2] = 2

// display
for (const i of items) {
  for (const j of i) {
    for (const k of j) {
      console.log('k = ', k)
    }
  }
}

1 Comment

i mean how to use it like java. eg: double a = new double[length][length][length];
1

Similar to rishat's answer, but has different length.

const array1 = Array(2).fill().map(e => Array(4).fill().map(e => Array(3).fill("3").map(e => e)));
console.log(JSON.stringify(array1));

Comments

0

I took this approach in my current project:

    const sourceImage = [1,2,3,255,4,5,6,255,7,8,9,255,10,11,12,255 ]; // RGBA - 16
    
    let noAlphaImage = [];
    
    for (var i = 0; i < sourceImage.length; i++)
    {       
        if (i % 4 !== 3)
        {
            noAlphaImage.push(sourceImage[i]); 
        }
    }
    
    console.log(noAlphaImage);
    
    const WIDTH = 2, HEIGHT = 2, PIXEL = 3;
    var destImage = [];
    
    for (var w = 0; w < WIDTH; w++)
    {
        destImage[w] = [];
        for (var h = 0; h < HEIGHT; h++)
        {
            destImage[w][h] = [];
            for (var p = 0; p < PIXEL; p++)
            {
                const destIndex = (w * HEIGHT * PIXEL) + (h * PIXEL) + (p);
                destImage[w][h][p] = noAlphaImage[destIndex];      
            }
        }
    }
    
    console.log(destImage);

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.