Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript unidimensional to multidimensional array
    primarykey
    data
    text
    <p>I have a unidimensional array filled up with graphic point coordinates that i get from a JSON encoded string, on which i </p> <pre><code>jsdata = $.parseJSON('[' + strdata + ']'); arr = jsdata[0].toString().split(','); </code></pre> <p>and i don't seem to quite think up the algorigthm to parse it to a 3 dimensional array, which needs to have the following structure:</p> <pre><code> data['parameter to plot'][point][coordinate] </code></pre> <p>As an example, let's say i'm gonna draw a line graph with data from 2 parameters. It will draw 74 points with 2 coordinates for each of the 2 lines which means the array's index will go as far as:</p> <pre><code>arr[296]; </code></pre> <p>So, the "data" array's indexes will need to go as far as:</p> <pre><code> data[1][74][1]; </code></pre> <p>I have no problem creating any of the arrays, but developing an algorithm for filling up the "data" array according to "arr's" length is really baking my brain. :/</p> <p>Thanks for any help bros.</p> <p>EDIT:</p> <p>Since i don't know how many parameters i'm gonna need to draw graphs for but i'll know which type of graph i'm going to use, i came up with this answer.</p> <p>I create a multi dimensional array using this function</p> <pre><code>function createArray(length) { var arr = new Array(length || 0), i = length; if (arguments.length &gt; 1) { var args = Array.prototype.slice.call(arguments, 1); while(i--) arr[i] = createArray.apply(this, args); } return arr; } </code></pre> <p>And then i can do</p> <pre><code>function toMulti(arr, plotType) { if(plotType =='line_plot') { var lineaux = createArray((arr.length/74/2),arr.length/2,2); var cont = 0; for(i= 0; i&lt;lineaux.length; i++) for(j=0; j&lt; lineaux[i].length; j++) for(k=0; k&lt; lineaux[i][j].length;k++) { lineaux[i][j][k] = arr[cont]; cont +=1; } return lineaux; } } </code></pre> <p>But a line graph will not always have 74 points, nor will i always plot line graph..</p> <p>Tell me what you think about it and if you can think up any way to make this non-plotType dependent i'd be eternally grateful. Or just buy you a beer to make it even. :D</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload