Migrating C # 3D Array to JS 3D Array

I developed the aC # library, which was used in three projects that relay in this particular piece of code, although I still need to use this code in javascript, so I port it, the thing is, I don’t think I can reproduce the same logic For example, I thought about this for several days, I could not answer this question.

In the C # library, I have a three-dimensional array, which, like the kernel property in the application, and I cannot figure out how to do this in the JS environment.

For example, I have this piece of code:

public Obj[,,] objs = new Obj[18, 14, 8]; 

In which I would select objects in EVERY one position and try to port this to javascript, it seems to result in:

 var 3dArr = new Array(); 3dArr[0] = new Array(); 3dArr[0][0] = new Array(); 

Does it always keep the array object in the first position, and if I set everything else that would lose the whole array? Or I'm wrong

+7
javascript c #
source share
4 answers

At the time of writing, there is no language function in JavaScript that would be very similar to multidimensional arrays in C #. It is also not recommended to expose regular arrays to use the entire application, since this allows you to erroneously damage all code by writing to the array that it should not hold.

However, to encapsulate an array in a simple new class should be relatively easy. Array3d in the code below:

 /***** CODE OF THE ARRAY *****/ function Array3d(width, height, depth, type) { this.width = width; this.height = height; this.depth = depth; this.type = type; this.array = new Array(width * height * depth); } Array3d.prototype = { get: function(x, y, z) { return this.array[calcIndex(this, x, y, z)]; }, set: function(x, y, z, item) { if (!(item instanceof this.type)) { throw new Error(item + ' is not instance of ' + this.type + '.'); } this.array[calcIndex(this, x, y, z)] = item; } }; function calcIndex(array3d, x, y, z) { return x + array3d.width * y + array3d.width * array3d.height * z; } /***** USAGE CODE *****/ function Something(i) { this.index = i; } var array3d = new Array3d(10, 11, 12, Something); var something = new Something(11); array3d.set(4, 0, 0, something); var gettingBack = array3d.get(4, 0, 0); if (gettingBack === something) { console.log('1: Works as expected'); } else { console.error('1: Not expected ' + gettingBack); } gettingBack = array3d.get(0, 0, 4); if (gettingBack == null) { console.log('2: Works as expected'); } else { console.error('1: Not expected ' + gettingBack); } 
+2
source share

Try it...

 Array3d = function (width, height, depth, defaultValue) { width = width || 1; height = height || 1; depth = depth || 1; defaultValue= defaultValue|| 0; var arr = []; for (var i = 0; i < width; i++) { arr.push([]); for (var j = 0; j < height; j++) { var last = arr[arr.length - 1]; last.push([]); for (var k = 0; k < depth; k++) { last[last.length - 1].push(defaultValue); } } } return arr; } console.log(new Array3d(3, 5, 2, 0)); 

So, specify width , height , depth (these three are 1 by default) and defaultValue (zero by default), and you can manipulate it just like any javascript array ...

+1
source share

In javascript, a 3D array is created as an array of arrays of arrays:

 var 3DArr = [[[],[],[]],[[],[],[]],[[],[],[]]]; 

now 3DArr is an array with arrays inside it, each of which also contains arrays. So you can do (for example):

 3DArr[0][1].push(10); console.log(3DArr); // [[[],[10],[]],[[],[],[]],[[],[],[]]] 
+1
source share

In new browsers, an array of arrays can be initialized using Array.from :

 a = Array.from(Array(3), () => Array.from(Array(3), () => Array(3).fill(0))) console.log(JSON.stringify(a)) 

As a side note to JavaScript, “Array” is more like a special Dictionary that uses its entire keys:

 a = [] a['1'] = 2 ab = 'c' a[3.14] = Math.PI console.log(a) // "[undefined, 2]" console.log(a[1], ab, a['3.14']) // "2 c 3.141592653589793" 

so that you can assign values ​​to it before initializing all the "dimensions":

 function set(a, x, y, z, v) { if(!a[x]) a[x] = [] if(!a[x][y]) a[x][y] = [] a[x][y][z] = v } a = [] set(a, 1, 2, 3, 4) console.log(a) 
0
source share

All Articles