Pretty print directory structure in a JSON recursively
You could take a single loop without reversing the array and hand over the prefix for the next level indentation.


const
    print = (data, indentation, prefix = '') =&gt; data
        .map(({ name, data }) =&gt; Array.isArray(data)
            ? prefix + name + '\n' + print(data, indentation, (prefix || '|') + '_'.repeat(indentation))
            : prefix + name
        )
        .join('\n'),
    dirStructure = [{ name: 'root_dir', data: [{ name: 'dir_a', data: [{ name: 'file_00.txt', data: 'file_00 content' }, { name: 'dir_b', data: [{ name: 'dir_c', data: [{ name: 'file_01.txt', data: 'file_01 content' }, { name: 'file_02.txt', data: 'file_02 content' }, { name: 'file_03.txt', data: 'file_03 content' }, { name: 'file_04.txt', data: 'file_04 content' }] }, { name: 'file_05.txt', data: 'file_05 content' }, { name: 'file_06.txt', data: 'file_06 content' }] }, { name: 'dir_d', data: [{ name: 'file_07.txt', data: 'file_07 content' }, { name: 'file_08.txt', data: 'file_08 content' }] }] }] }],
    result = print(dirStructure, 2);

   console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }