Update 3d matrix with coordinates in javascript -
i'm trying create 3d matrix in javascript, , want update values using coordinates:
var xaxis = []; var yaxis = []; var zaxis = []; var dimensions = 4; //initalize matrix (var i=0;i<dimensions;i++){ xaxis.push(0); } (var j=0;j<dimensions;j++){ yaxis.push(xaxis); } (var k=0;k<dimensions;k++){ matrix.push(yaxis); } //check value of 1 point in matrix matrix[1][2][3]; //returns 0 expected //update value of matrix using same coordinates matrix[1][2][3] = 2;
when run last step, not update expected 1[2][3] coordinates, updates every third value of every array 2 see in image:
how can manage update value want?? mean, number 2 should in 1 coordinate point, in case 1[2][3].
note: think it's got might assigning original arrays matrix instead of new copies of themselves, when update 1 coordinate, i'm updating original array, , other arrays pointing @ original, that's why it's been reflected there well?
you push arrays reference.
consider creating matrix way:
var matrix = [0, 1, 2, 3].map(function() { return new array(4).fill().map(function(){ return new array(4).fill(0); }); });
Comments
Post a Comment