Tree

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pietanczyk

    Tree

    I have the following array:

    var arrTreeLeaves = ['Root', ['1', ['1.1', '1.2', '1.3']],
    ['2', ['2.1', '2.2', '2.3']],
    ['3', ['3.1', '3.2', '3.3']]];

    I would like to get the following tree structure:

    Root
    1
    1.1
    1.2
    1.3
    2
    2.1
    2.2
    2.3
    3
    3.1
    3.2
    3.3

    Could someone help me and write a recursive function which would do that?
  • Grant Wagner

    #2
    Re: Tree

    pietanczyk wrote:
    [color=blue]
    > I have the following array:
    >
    > var arrTreeLeaves = ['Root', ['1', ['1.1', '1.2', '1.3']],
    > ['2', ['2.1', '2.2', '2.3']],
    > ['3', ['3.1', '3.2', '3.3']]];
    >
    > I would like to get the following tree structure:
    >
    > Root
    > 1
    > 1.1
    > 1.2
    > 1.3
    > 2
    > 2.1
    > 2.2
    > 2.3
    > 3
    > 3.1
    > 3.2
    > 3.3
    >
    > Could someone help me and write a recursive function which would do that?[/color]

    <script type="text/javascript">
    var arrTreeLeaves = ['Root', ['1', ['1.1', '1.2', '1.3']],
    ['2', ['2.1', '2.2', '2.3']],
    ['3', ['3.1', '3.2', '3.3']]];

    function dump(array, level) {
    if (array && array.construct or == Array) {

    level = +level || 0;

    for (var ii = 0; ii < array.length; ++ii) {
    if (array[ii].constructor == Array) {
    dump(array[ii], level + 1);
    } else {
    for (var jj = 0; jj < level; ++jj) {
    document.write( '&nbsp; &nbsp;');
    }
    document.write( array[ii] + '<br>');
    }
    }
    }
    }

    dump(arrTreeLea ves);
    </script>

    --
    Grant Wagner <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq

    Comment

    Working...