Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating an multidimensional Array from string (hierarchical data)
    text
    copied!<p>I have an Array with this kind of values:</p> <pre><code>val = [ ['L-2-4-1','john','bla1'], ['L-1-1-26','bohn','bla2'], ['L-2-1','cohn','bla3'], ['L-1-1-05','rohn','bla4'], ['L-1-1','gohn','bla5'] ['L-2-3-1','zohn','bla-finally'] ]; </code></pre> <p>The number-sequence is always unique and "0" is never used.<br> What I'm trying to get would be something like this:</p> <pre><code>ser = [ [undefined], [ [undefined],[ ['gohn'],['bla5'] ], [undefined], ... , [ ['bohn'], ['blah2'] ] ], ... ]; </code></pre> <p>The purpose is to be able to access the data like this:</p> <pre><code>ser[2][4][1][0]; // Array('john','bla1') ser[1][1][0]; // Array('gohn','bla5') ser[1][1][26][0]; // Array('bohn','bla2') </code></pre> <p>and also to loop through all elements.. for instance:</p> <pre><code>for(var i = 0; i &lt;= ser[1][1].length; i++){ //code } </code></pre> <p>The main problem I have is that I was not able to set the variables the same way I intend to read them. Because this does NOT work, since I need to declare all arrays separately as arrays (right?)</p> <pre><code>var ser[1][1][26][0] = ['john','bla1']; // Nop; </code></pre> <p>I don't know the maximum depth of the tree</p> <p>Trying to build the arrays from "inside out" or from "right to left" -however it is best described- I always end up overwriting previously set array elements.</p> <p>Maybe the whole idea is too complicated (or at least not ideal) for the purpose? What would you suggest? I have the feeling I´m trying to do the right thing but the wrong way... Something like organizing marbles on a glass surface. Everything keeps moving around... </p>
 

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