2017-03-14 2 views
1

Ich habe Array mit Namen von, sagen wir, abhängigen Objekten. Das Problem ist, dass das abhängige Objekt auch abhängige Objekte haben kann und so weiter. Mein Code ist nur 2 Ebenen tief und ich muss eine rekursive Art und Weise implementieren. Mein Code ist:Javascript rekursive Funktion und Lodash

if (obj.detailsObjects) { 
      _.forEach(obj.detailsObjects, function (o, key) { 
       if (window[o] instanceof QuadTable) { 
        //limpamos o details e reiniciamos a info 
        if ($.fn.DataTable.isDataTable('#' + window[o].tableId)) { 
         debugger; 
         window[o].tbl.clear(); 
         window[o].tbl.columns.adjust().draw(); 

         $('#' + window[o].tableId + "_info > .nRecords").text(window[o].tbl.data().count() + " " + window[o].i18nEntries.record); 
         if (window[o].editorXt) { 
          $('#' + window[o].tableId + '_xtForm')[0].reset(); 
         } 
        } 
        //se o details for um master repetimos a operação para o details do details 
        //todo recursividade 
        if (window[o].detailsObjects) { 
         _.forEach(window[o].detailsObjects, function (ob, key) { 
          if (window[ob] instanceof QuadTable) { 
           if ($.fn.DataTable.isDataTable('#' + window[ob].tableId)) { 
            window[ob].tbl.clear(); 
            window[ob].tbl.columns.adjust().draw(); 
            $('#' + window[ob].tableId + "_info > .nRecords").text(window[ob].tbl.data().count() + " " + window[ob].i18nEntries.record); 
            if (window[ob].editorXt) { 
             $('#' + window[ob].tableId + '_xtForm')[0].reset(); 
            } 
           } 
          } else if (window[ob] instanceof QuadForm) { 
           window[ob].clearForm(); 
          } 
          window[ob].startIn = 0; 
         }); 
        } 
       } else if (window[o] instanceof QuadForm) { 
        window[o].clearForm(); 
       } 
       window[o].startIn = 0; 
      }); 
     } 

Antwort

1
function recursiveFuntcion(obj) { 
    // base condition 
    if (!obj || !obj.detailsObjects) 
    return; 
    _.forEach(obj.detailsObjects, function(o, key) { 
    if (window[o] instanceof QuadTable) { 
     //limpamos o details e reiniciamos a info 
     if ($.fn.DataTable.isDataTable('#' + window[o].tableId)) { 
     debugger; 
     window[o].tbl.clear(); 
     window[o].tbl.columns.adjust().draw(); 

     $('#' + window[o].tableId + "_info > .nRecords").text(window[o].tbl.data().count() + " " + window[o].i18nEntries.record); 
     if (window[o].editorXt) { 
      $('#' + window[o].tableId + '_xtForm')[0].reset(); 
     } 
     } 
     //recursive call 
     recursiveFunction(window[o]); 
    } else if (window[o] instanceof QuadForm) { 
     window[o].clearForm(); 
    } 
    window[o].startIn = 0; 
    }); 
} 
+0

Dank Sie Tln. –