作法:使用AngularJS递归遍历清单和子清单,寻找资料

时间:2018-07-21 05:59:46

标签: javascript angularjs

我今晚写了一个极其可怕的超级嵌套循环,我只知道必须有一种更好的方法。因此,基础知识:

  • orgId = a Guid(可能没有破折号,因为有时我的棱角喜欢掉线)
  • rootOrgList = Id Guid)和 Name String)和 ParentOrgId的列表Guid

目标:在rootOrgList中查找orgId的任何实例,并为该parentOrgId获取orgId

现在,此循环有效,并且由于全部用于循环,因此实际上运行相当快,但是它的时间太长,嵌套的方式太愚蠢了,而且这样做的方法也太愚蠢了,但是我很累,并且烦死了,不能再不想把头撞在桌子上了。

那么,有没有人有想法以更好的方式实现预期的结果? (下面的循环,顺便说一句,将使您对数据的结构有一个很好的了解,这就是为什么我没有提供JSON的原因。)

vm.getParentOrgId = (
  function(orgId, rootOrgList) {
    var parentOrgId = "";
    var curParentOrgId = orgId;

    //#region Check the Root Level list
    var idxLvlRoot = indexOfDashlessStringInArray(rootOrgList, guidifyString(orgId), "Id");
    if (idxLvlRoot > -1) {
      //Found it? Great! We're done. Return the org we came in with!
      return guidifyString(orgId);
    }
    //#endregion

    //#region If it's not in the root level, check for sub-lists
    for (var orgRoot = 0; orgRoot < rootOrgList.length; orgRoot++) {
      curParentOrgId = rootOrgList[orgRoot].Id;

      if (hasValue(rootOrgList[orgRoot].Organizations)) {
        var orgSubListLvl1 = rootOrgList[orgRoot].Organizations;

        //#region Check the Sub list for it
        var idxSubLvl1 = indexOfDashlessStringInArray(orgSubListLvl1, guidifyString(orgId), "Id");
        if (idxSubLvl1 > -1) {
          //Found it? Great! We're done. Return it!
          return guidifyString(curParentOrgId);
        }
        //#endregion

        //#region Not there? Look deeper, if you can
        for (var orgSubLvl1 = 0; orgSubLvl1 < orgSubListLvl1.length; orgSubLvl1++) {
          curParentOrgId = orgSubListLvl1[orgSubLvl1].Id;

          if (hasValue(orgSubListLvl1[orgSubLvl1].Organizations)) {
            var orgSubListLvl2 = orgSubListLvl1[orgSubLvl1].Organizations;

            //#region Check the Sub list for it
            var idxSubLvl2 = indexOfDashlessStringInArray(orgSubListLvl2, guidifyString(orgId), "Id");
            if (idxSubLvl2 > -1) {
              //Found it? Great! We're done. Return it!
              return guidifyString(curParentOrgId);
            }
            //#endregion

            //#region Not there? Look deeper, if you can
            for (var orgSubLvl2 = 0; orgSubLvl2 < orgSubListLvl2.length; orgSubLvl2++) {
              curParentOrgId = orgSubListLvl2[orgSubLvl2].Id;

              if (hasValue(orgSubListLvl2[orgSubLvl2].Organizations)) {
                var orgSubListLvl3 = orgSubListLvl2[orgSubLvl2].Organizations;

                //#region Check the Sub list for it
                var idxSubLvl3 = indexOfDashlessStringInArray(orgSubListLvl3, guidifyString(orgId), "Id");
                if (idxSubLvl3 > -1) {
                  //Found it? Great! We're done. Return it!
                  return guidifyString(curParentOrgId);
                }
                //#endregion

                //#region Not there? Look deeper, if you can
                for (var orgSubLvl3 = 0; orgSubLvl3 < orgSubListLvl3.length; orgSubLvl3++) {
                  curParentOrgId = orgSubListLvl3[orgSubLvl3].Id;

                  if (hasValue(orgSubListLvl3[orgSubLvl3].Organizations)) {
                    var orgSubListLvl4 = orgSubListLvl3[orgSubLvl3].Organizations;

                    //#region Check the Sub list for it
                    var idxSubLvl4 = indexOfDashlessStringInArray(orgSubListLvl4, guidifyString(orgId), "Id");
                    if (idxSubLvl4 > -1) {
                      //Found it? Great! We're done. Return it!
                      return guidifyString(curParentOrgId);
                    }
                    //#endregion

                    //#region Not there? Look deeper, if you can
                    for (var orgSubLvl4 = 0; orgSubLvl4 < orgSubListLvl4.length; orgSubLvl4++) {
                      curParentOrgId = orgSubListLvl4[orgSubLvl4].Id;

                      if (hasValue(orgSubListLvl4[orgSubLvl4].Organizations)) {
                        var orgSubListLvl5 = orgSubListLvl4[orgSubLvl4].Organizations;

                        //#region Check the Sub list for it
                        var idxSubLvl5 = indexOfDashlessStringInArray(orgSubListLvl5, guidifyString(orgId), "Id");
                        if (idxSubLvl5 > -1) {
                          //Found it? Great! We're done. Return it!
                          return guidifyString(curParentOrgId);
                        }
                        //#endregion

                        //#region Not there? Look deeper, if you can
                        for (var orgSubLvl5 = 0; orgSubLvl5 < orgSubListLvl5.length; orgSubLvl5++) {
                          curParentOrgId = orgSubListLvl5[orgSubLvl5].Id;

                          if (hasValue(orgSubListLvl5[orgSubLvl5].Organizations)) {
                            var orgSubListLvl6 = orgSubListLvl5[orgSubLvl5].Organizations;

                            //#region Check the Sub list for it
                            var idxSubLvl6 = indexOfDashlessStringInArray(orgSubListLvl6, guidifyString(orgId), "Id");
                            if (idxSubLvl6 > -1) {
                              //Found it? Great! We're done. Return it!
                              return guidifyString(curParentOrgId);
                            }
                            //#endregion

                            //#region Not there? Look deeper, if you can
                            for (var orgSubLvl6 = 0; orgSubLvl6 < orgSubListLvl6.length; orgSubLvl6++) {
                              curParentOrgId = orgSubListLvl6[orgSubLvl6].Id;

                              if (hasValue(orgSubListLvl6[orgSubLvl6].Organizations)) {
                                var orgSubListLvl7 = orgSubListLvl6[orgSubLvl6].Organizations;

                                //#region Check the Sub list for it
                                var idxSubLvl7 = indexOfDashlessStringInArray(orgSubListLvl7, guidifyString(orgId), "Id");
                                if (idxSubLvl7 > -1) {
                                  //Found it? Great! We're done. Return it!
                                  return guidifyString(curParentOrgId);
                                }
                                //#endregion

                                //#region Not there? Look deeper, if you can
                                for (var orgSubLvl7 = 0; orgSubLvl7 < orgSubListLvl7.length; orgSubLvl7++) {
                                  curParentOrgId = orgSubListLvl7[orgSubLvl7].Id;

                                  if (hasValue(orgSubListLvl7[orgSubLvl7].Organizations)) {
                                    var orgSubListLvl8 = orgSubListLvl7[orgSubLvl7].Organizations;

                                    //#region Check the Sub list for it
                                    var idxSubLvl8 = indexOfDashlessStringInArray(orgSubListLvl8, guidifyString(orgId), "Id");
                                    if (idxSubLvl8 > -1) {
                                      //Found it? Great! We're done. Return it!
                                      return guidifyString(curParentOrgId);
                                    }
                                    //#endregion

                                    //#region Not there? Look deeper, if you can
                                    for (var orgSubLvl8 = 0; orgSubLvl8 < orgSubListLvl8.length; orgSubLvl8++) {
                                      curParentOrgId = orgSubListLvl8[orgSubLvl8].Id;

                                      if (hasValue(orgSubListLvl8[orgSubLvl8].Organizations)) {
                                        var orgSubListLvl9 = orgSubListLvl8[orgSubLvl8].Organizations;

                                        //#region Check the Sub list for it
                                        var idxSubLvl9 = indexOfDashlessStringInArray(orgSubListLvl9, guidifyString(orgId), "Id");
                                        if (idxSubLvl9 > -1) {
                                          //Found it? Great! We're done. Return it!
                                          return guidifyString(curParentOrgId);
                                        }
                                        //#endregion

                                        //#region Not there? Look deeper, if you can
                                        for (var orgSubLvl9 = 0; orgSubLvl9 < orgSubListLvl9.length; orgSubLvl9++) {
                                          curParentOrgId = orgSubListLvl9[orgSubLvl9].Id;

                                          if (hasValue(orgSubListLvl9[orgSubLvl9].Organizations)) {
                                            var orgSubListLvl10 = orgSubListLvl9[orgSubLvl9].Organizations;

                                            //#region Check the Sub list for it
                                            var idxSubLvl10 = indexOfDashlessStringInArray(orgSubListLvl10, guidifyString(orgId), "Id");
                                            if (idxSubLvl10 > -1) {
                                              //Found it? Great! We're done. Return it!
                                              return guidifyString(curParentOrgId);
                                            }
                                            //#endregion

                                            //At this point, if we still haven't found it, we aren't going to.
                                          }
                                        }
                                        //#endregion
                                      }
                                    }
                                    //#endregion
                                  }
                                }
                                //#endregion
                              }
                            }
                            //#endregion
                          }
                        }
                        //#endregion
                      }
                    }
                    //#endregion
                  }
                }
                //#endregion
              }
            }
            //#endregion
          }
        }
        //#endregion
      }
    }
    //#endregion

    return parentOrgId;
  }
);

更新 看完@adolfotcar的回答后,我修改了代码以引入递归来解决问题。结果如下:

vm.getParentOrgId = (
  function(orgId, rootOrgList) {
    var parentOrgId = "";
    var curParentOrgId = orgId;

    //#region Check the Root Level list
    var idxLvlRoot = indexOfDashlessStringInArray(rootOrgList, guidifyString(orgId), "Id");
    if (idxLvlRoot > -1) {
      //Found it? Great! We're done. Return the org we came in with!
      return guidifyString(orgId);
    }
    //#endregion

    //#region If it's not in the root level, check for sub-lists
    for (var orgRoot = 0; orgRoot < rootOrgList.length; orgRoot++) {
      if (hasValue(rootOrgList[orgRoot].Organizations)) {
        curParentOrgId = rootOrgList[orgRoot].Id;

        //#region Check the Sub list for it
        var idxSubLvl1 = indexOfDashlessStringInArray(rootOrgList[orgRoot].Organizations, guidifyString(orgId), "Id");
        if (idxSubLvl1 > -1) {
          //Found it? Great! We're done. Return it!
          parentOrgId = guidifyString(curParentOrgId);
          return parentOrgId;
        }
        //#endregion

        //If we still haven't found it, recurse the method call to go deeper.
        parentOrgId = vm.getParentOrgId(orgId, rootOrgList[orgRoot].Organizations);
      }
    }
    //#endregion

    return parentOrgId;
  }
);

1 个答案:

答案 0 :(得分:1)

好吧,假设您的rootOrgList类似于:

$scope.list = [ 
                {id: 1, name: "a", parentId: 0, 
                    organizations: [
                                        {id: 2, name: "b", parentId: 1, organizations: [{id: 7, name: "g", parentId: 2, organizations: {}}]},
                                        {id: 3, name: "c", parentId: 1, organizations: []},
                                        {id: 4, name: "d", parentId: 1, 
                                            organizations: [
                                                                {id: 5, name: "e", parentId: 4, organizations: {}},
                                                                {id: 6, name: "f", parentId: 4, organizations: {}}
                                                            ]
                                        }
                                    ]
                }
            ];

然后此函数应该执行:

$scope.getParentId = function(haystack, needle){
    //it'll return 0 if not found
    var parentId = 0;
    angular.forEach(haystack, function(organization, key){
        if (organization.id==needle) {
            parentId = organization.parentId;
            return;// this return breaks only the foreach
        }
        //if parentId was found in the previous forEach then just keeps that value, if not then keeps looking in next sublevel
        parentId = parentId ? parentId : $scope.getParentId(organization.organizations, needle); 
    });
    return parentId;
}

这样调用函数:

var parentId = $scope.getParentId($scope.list, 4);

显然,您需要将代码调整为变量名和其他小位数,但是应该这样做。