Underscore.js has some nice method working with arrays maybe you can use flatten and filter to create one array from your structure then you can just write one foreach:
Or you could use templates to encapsulate your if: viewable logic and apply the template recursively: