When ordering a recursive function, arrays of arrays appear

I am currently dealing with the problem of writing a recursive function to order some json data. I have several nested arrays of objects that I need to order as separate slides. The structure is similar to the following:

[ { "title": "a", "children": [ { "title": "aa", "children": [ { "title": "aaa" }, { "title": "aab" } ] }, { "title": "ab", "children": [ { "title": "aba" }, { "title": "abb" } ] } ] }, { "title": "b", "children": [ { "title": "ba", "children": [ { "title": "baa" }, { "title": "bab" } ] }, { "title": "bb", "children": [ { "title": "bba" }, { "title": "bbb" } ] } ] } ] 

I wrote a recursive function:

 var catalog = { init: function() { var _this = this; $.getJSON("catalog.json", function(data) { _this.slides = []; _this.parseCategories(data.catalog.category,-1,0); }); }, parseCategories: function(array, depth, prevParent) { ++depth; if (!this.slides[depth]) this.slides[depth] = []; if (!this.slides[depth][prevParent]) this.slides[depth][prevParent] = []; this.slides[depth][prevParent].push(array); for (var i = 0; i < array.length; i++) { if (array[i].category) { this.parseCategories(array[i].category, depth, i); } } } } catalog.init(); 

It is output:

enter image description here

However, instead of getting data for my third slide in the format:

aah

aba

a-s-a

I would like to get

a-a- [a, b, c]

I was wondering if this is possible, since I am not very good at recursive processes. I hope I was clean and thank you for that. I basically need to keep the original data structure, but remove the first level of depth for each iteration (a slide in the slider that reflects the increase in depth in my data structure).

+7
javascript arrays recursion
source share
1 answer

I recently wrote an algorithm to recursively process such data. Here is jsfiddle and main function

 console.log('starting'); // data in tree format. var output = {}; // data in slide format ["aaa", "aab", "bba", "bbb"] var outputStrs = []; parseData(data, output); console.log(output); console.log(outputStrs); function parseData(data, store) { // go through each element for (var i = 0; i < data.length; i++) { var element = data[i]; // used to keep track of where we are in the tree. var splitElement = element.title.split('-'); var titleStart = splitElement[0]; // console.log(element); if (_.has(element, 'children') && _.isArray(element.children)) { // if there is a children, then recursively handle it. store[titleStart] = {}; parseData(element.children, store[titleStart]); } else { // if we are at the end, then add in the data differently. var titleEnd = splitElement[splitElement.length-1]; store[titleEnd] = titleEnd; // create the slides var slide = []; for (var j = 0; j < splitElement.length; j++) { if (j !== splitElement.length - 1) { slide.push(titleStart); } else { slide.push(titleEnd); } } slide = slide.join('-'); if (!_.contains(outputStrs, slide)) outputStrs.push(slide); } } } 

With this data, the output should resemble

 a a a b b b a b 

And outputStrs will look like aa- [a, b, c]

Hope this helps !!!

0
source share

All Articles