I have a JSON file that requires parsing. I'm attempting to implement a recursive method for this task. The current JSON data is structured as shown below:
Item 01
SubItem 01
InnerSubItem 01
Item 02
SubItem 01
InnerSubItem 01
Unfortunately, the function I wrote only manages to parse the first set of data (The contents under Item 01). It seems like the code does not loop back when the condition is false.
Here is the code snippet I used:
$.getJSON('https://api.myjson.com/bins/6atbz', function(data) {
repeat(data, data.layers);
})
function repeat(data, x) {
var layer = data.layers.reverse()
for (i = 0; i < x.length; i++) {
name = x[i].name
console.log(name)
if (x[i].layers.length > 0) {
repeat(data, x[i].layers)
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>