I have an array like:
"pages": [{
"key": "1",
"pages": [{
"key": "2",
"pages": [{
"key": "3"
}]
},
{
"key": "4",
"pages": [{
"key": "5"
}]
}]
}]
where key 1 and 4 are at same level and 1 contains 2 which contains 3 and the key 4 contains 5. The result I want is in the order [3,2,5,1,4]. I have tried the following recursion but i am not able to get the correct order.
function fnGetAll (oTopDetailPage, array) {
var i;
for (i=0; i<oTopDetailPage.length; i++) {
array.push(oTopDetailPage[i]);
if(oTopDetailPage[i].pages) {
fnGetAllSubPages(oTopDetailPage[i].pages, array);
}
}
return array;
}