Friday, 15 May 2015

TypeError: matrix[y].push is not a function when trying to create 2d array with javascript -


I am trying to create an array that represents a matrix (256 × 256), is full of zero Which I will change later with the works I tried to use for loops and .push function, but something is wrong and I do not know what is happening. Here is my code

  var matrix = []; For (x = 0; x  

I have to do this, push an empty array in the matrix, fill it with 256 zeros, and then repeat 255 more times after that.

The problem is that you are pushing only one array, 255 of them are trying to fill Are there. Your code should look like this (assuming you want to give 256 instead of 255)

< Div class = "snippet-code">
  var matrix = []; For (var x = 0; x & lt; 256; x ++) {matrix peash ([]); For (var y = 0; y & lt; 256; y ++) {matrix [x]. Push (0); }}  


No comments:

Post a Comment