I have the following function using in my application:
applyFormSectionIdsToControls: function(formSections) {
if (!formSections.length) return;
var formSectionIds = [];
for (var i = 0, ii = formSections.length; i < ii; i++) {
formSectionIds.push(parseInt(formSections[i].id));
};
for (var i = 0, ii = formSections.length; i < ii; i++) {
var affectedWidgets = $.grep(application_widgets, function(item) {
return (($.inArray(item.widget_key, ['nextbutton', 'dropdown', 'radiobuttons']) >= 0) && (item.parent_id == formSections[i].id));
});
for (var j = 0, jj = affectedWidgets.length; j < jj; j++) {
if (affectedWidgets[j].widget_key == 'nextbutton' && (typeof affectedWidgets[j].settings.default_next_step != 'undefined' && affectedWidgets[j].settings.default_next_step > 0)) {
var _next_step = parseInt(affectedWidgets[j].settings.default_next_step);
if ($.inArray(_next_step, formSectionIds) == -1) {
affectedWidgets[j].settings.default_next_step = formSectionIds[_next_step - 1];
}
}
if (affectedWidgets[j].widget_key == 'radiobuttons' || affectedWidgets[j].widget_key == 'dropdown') {
for (var k = 0, kk = affectedWidgets[j].settings.answers; k < kk; k++) {
var _next_step = parseInt(affectedWidgets[k].settings.answers[k].next_step);
if ($.inArray(_next_step, formSectionIds) == -1) {
affectedWidgets[k].settings.answers[k].next_step = formSectionIds[_next_step - 1];
}
}
}
}
};
}
I'm looking for any hints how to refactor it, as I think there are too many for loops.
for-loops ... sure there is some optimizations that I see, but ... \$\endgroup\$