How do I create a 3d array in JavaScript initialised to be full of zeroes, with XYZ dimensions 5x5x64

24 views Asked by At

I've written what i thought would create an array of 5 arrays each containing 5 arrays of 64 zeroes, but when i write the resulting array to the console, i do not get what i expected to see, yet the code makes sense to me.

Here is my code:

    const C = new Array(5);

    for (let x = 0; x < 5; x++) {
        for (let y = 0; y < 5; y++) {
            for (let z = 0; z < 64; z++) {
                C[x,y,z] = 0;
            }
        }
    }

    console.log(C);

logging C presents me with one array of 64 zeroes. What is wrong?

1

There are 1 answers

0
Alexander Nenashev On

You could use Array.from():

const result = Array.from({length:5}, () => Array.from({length:5}, () => Array(64).fill(0)));

console.log(JSON.stringify(result));