JSFiddle - React, Tailwind, and code Playground

by patrick_dw

HTML

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/themes/ui-lightness/jquery-ui.css">

JavaScript

(function(){
    var arr = [
        'one',
        [
            'two',
            'three',
            [
                'four',
                'five',
                'six'
            ],
            'seven'
        ],
        'eight',
        'nine'
    ];
    var current = arr;
    var idx = 0;
    var array_depth = [{ idx: idx, arr: current}];
    var depth_index = 0;
//    debugger;
    outer: while (idx < current.length) {
    
        if ( Array.isArray( current[idx] ) ) {
            current = current[ idx ];
            idx = 0;
            depth_index++;
            array_depth[ array_depth.length - 1 ].idx++;
            array_depth.push({ idx: idx, arr: current });
        } else {
            console.log( current[ idx ] );
            array_depth[ array_depth.length - 1 ].idx = ++idx;
            if ( idx === current.length ) {
                current = array_depth[ array_depth.length - 1 ];
                if ( current ) {
                    idx = current.idx;
                    current = current.arr;
                    array_depth.length = array_depth.length - 1;
                    depth_index--;
                } else {
                    ; // the outer loop is done
                }
            }
        }
    }
})();