0

I want initialize an array 3d with zeros in all positions. i am using a for cycle for filling my matrix but when I tested, I get an error TypeError: this.matrix[x][y] is undefined I have this:

class Cube {

    constructor(size) {
        this.size = size
        this.matrix = [ [ [] ], [ [] ] ]
        this.createMatrix(size)
    }

    getsize() {
        return this.size
    }

    /*Fill matrix with zeros*/
    createMatrix(size) {
        for (var x = 0; x < size; x++) {
            for (var y = 0; y < size ; y++) {
                for (var z = 0; z < size ; z++) {
                   this.matrix[x][y][z] = 0
                }
            } 
        }
        console.log(this.matrix)
    }
 }

 myCube = new Cube(4)

How Can I fill my matrix?

3 Answers 3

2

You will have to check every item in the array if it wasn't initialized yet, and in such case you will need to set it to Array:

class Cube {

  constructor(size) {
    this.size = size
    this.matrix = [ ]
    this.createMatrix(size)
  }

  getsize() {
    return this.size
  }

  /*Fill matrix with zeros*/
  createMatrix(size) {
    for (var x = 0; x < size; x++) {
      for (var y = 0; y < size ; y++) {
        for (var z = 0; z < size ; z++) {
          if (!Array.isArray(this.matrix[x])) {
            this.matrix[x] = []
          }
          if (!Array.isArray(this.matrix[x][y])) {
            this.matrix[x][y] = []
          }
          if (!Array.isArray(this.matrix[x][y][z])) {
            this.matrix[x][y][z]= []
          }
          this.matrix[x][y][z] = 0
        }
      } 
    }
    console.log(this.matrix)
  }
}

myCube = new Cube(4)

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

3 Comments

Note: typeof is not a function so parentheses are not required and typeof [] will return "object" so it's not a good method for checking if something is an array.
@MikeC thanks for the comment! Changed to Array.isArray instead of typeof
Thanks @Dekel, your answer was useful for me.
2

That's because you need to initialize each individual array. Remember: a 3D array is an array of arrays of arrays.

function threeDArray(width, height, depth) {
  var result = [];
  for (var x = 0; x < width; x++) {
    result[x] = [];
    for (var y = 0; y < height; y++) {
      result[x][y] = [];
      for (var z = 0; z < depth; z++) {
        result[x][y][z] = 0;
      }
    }
  }
  return result;
}

console.log(threeDArray(3, 3, 3));

Comments

2

You can use Array.from({ length: n }) to create empty arrays of n length, and fill them with zeros using the fill() method:

const emptyArray = n => Array.from({ length: n });

const createMatrix = n => emptyArray(n).map(() =>
  emptyArray(n).map(() => emptyArray(n).fill(0))
);

console.log(createMatrix(4));

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.