I'm trying to get a result looking something like this: Miniors | Boys | 54kg - 62kg
where every value delimited by a pipe | comes from an array containing a certain "type of restriction". For example: ageGroups, genders, weightClasses
(as seen above).
The way I'm able to get this result right now is if I hard code the nested forEach-loops (using underscorejs), but this means I have to now how many arrays I have to loop over to get wanted result. This works "fine":
var categories = [];
_.each(ageGroups, function(ageGroup) {
_.each(gender, function(gender) {
_.each(weightClasses, function(weightClass) {
categories.push(ageGroup.name + ' | ' + gender.name + ' | ' + weightClass.name);
});
});
});
The output is an array (categories) with all the possible combinations of the restriction arrays.
Now, my problem is that I need a way to do the same with an unknown number of restriction arrays. My guess for a proper solution is recursion, BUT I haven't been able to produce anything that actually works since I'm not able to wrap my head around recursion just yet :)
A fiddle prepared with some test data can be found here: jsFiddle. The fiddle uses angular for some simple databinding and debugging the result output and underscorejs for handling the arrays.