Tree

P

pietanczyk

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?
 
G

Grant Wagner

pietanczyk said:
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?

<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.constructor == 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(arrTreeLeaves);
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top