How to call a recursive function in javascript

Murali

In my componentList I am going to have multiple objects.

if ($scope.componentList && $scope.componentList.length > 0) {
       angular.forEach($scope.componentList, function(admincomp, index) {
          $scope.validateAdmincomp(admincomp, index);
       });
 }



$scope.validateAdmincomp = function(admincomp, index) {
     for (var key in admincomp) {
       if (key !== "$$hashKey" && admincomp.hasOwnProperty(key)) {
           angular.element(document.querySelector('#' + key + index)).removeClass("errorhilight");
                }
            }
       if (admincomp.componentName == undefined || admincomp.componentName == "") {    
             angular.element(document.querySelector('#componentName' + index)).addClass("errorhilight");
                isValidData = false;
            }
};

The $scope.componentList format is going to be as follows

[
  {
    "revision": 0,  
    "componentName": "abc",
    "componentIdentification": "abc",
    "componentType": "1",
    "componentState": "1",
    "componentUrl": null,
    "componentId": "214",
    "rowId": "3",
    "items": [
      {
        "revision": 0,
        "componentName": "efg",
        "componentIdentification": "efg",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "215",
        "rowId": "3.1",
        "items": null,
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "16",
        "actionToPerform": "1"
      }
    ],
    "componentStateId": 0,
    "ctastatus": 0,
    "actionId": "37",
    "actionToPerform": "1"
  },
  {
    "revision": 0,
    "componentName": "hij",
    "componentIdentification": "hij",
    "componentType": "1",
    "componentState": "1",
    "componentUrl": null,
    "componentId": "206",
    "rowId": "1",
    "items": [
      {
        "revision": 0,
        "componentName": "klm",
        "componentIdentification": "klm",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "207",
        "rowId": "1.1",
        "items": [
          {
            "revision": 0,
            "componentName": "nop",
            "componentIdentification": "nop",
            "componentType": "2",
            "componentState": "1",
            "componentUrl": null,
            "componentId": "208",
            "rowId": "1.1.1",
            "items": [
              {
                "revision": 0,
                "componentName": "qrs",
                "componentIdentification": "qrs",
                "componentType": "2",
                "componentState": "1",
                "componentUrl": null,
                "componentId": "209",
                "rowId": "1.1.1.1",
                "items": null,
                "componentStateId": 0,
                "ctastatus": 0,
                "actionId": "26",
                "actionToPerform": "1"
              },
              {
                "revision": 0,
                "componentName": "tuv",
                "componentIdentification": "tuv",
                "componentType": "2",
                "componentState": "1",
                "componentUrl": null,
                "componentId": "210",
                "rowId": "1.1.1.2",
                "items": null,
                "componentStateId": 0,
                "ctastatus": 0,
                "actionId": "5",
                "actionToPerform": "1"
              }
            ],
            "componentStateId": 0,
            "ctastatus": 0,
            "actionId": "25",
            "actionToPerform": "1"
          }
        ],
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "1",
        "actionToPerform": "1"
      }
    ],
    "componentStateId": 0,
    "ctastatus": 0,
    "actionId": "37",
    "actionToPerform": "1"
  },
  {
    "revision": 0,
    "componentName": "wxy",
    "componentIdentification": "wxy",  
    "componentType": "1",
    "componentState": "1",
    "componentUrl": null,
    "componentId": "211",
    "rowId": "2",
    "items": [
      {
        "revision": 0,
        "componentName": "zab",
        "componentIdentification": "zab",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "212",
        "rowId": "2.1", 
        "items": null,
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "7",
        "actionToPerform": "1"
      },
      {
        "revision": 0,
        "componentName": "cde",
        "componentIdentification": "cde",
        "componentType": "2",
        "componentState": "1",
        "componentUrl": null,
        "componentId": "213",
        "rowId": "2.2",
        "items": null,
        "componentStateId": 0,
        "ctastatus": 0,
        "actionId": "12",
        "actionToPerform": "1"
      }
    ],
    "componentStateId": 0,
    "ctastatus": 0,
    "actionId": "37",
    "actionToPerform": "1"
  }
]

In the above code the parents are only validated as the forEach loop is considering only the $scope.componentList list and not considering the inside items[] list. I want to call validateAdmincomp function for each object. How can I call validateAdmincomp function forEach object.

Sachet Gupta

Use the below code. Move your iteration logic to a method (recursive method)

if ($scope.componentList && $scope.componentList.length > 0) {
    validateList($scope.componentList) // starting point to iterate and validate the list
}

var validateList = function(list) {
    angular.forEach(list, function(admincomp, index) {
        $scope.validateAdmincomp(admincomp, index); // considering this method is doing some other validations
        if (admincomp.items && admincomp.items.lenght > 0)
            validateList(admincomp.items); // method calling itself
    });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript: How to write and call a recursive function?

From Dev

From a cloud function: how to call JavaScript function that is recursive?

From Dev

From a cloud function: how to call JavaScript function that is recursive?

From Dev

JavaScript recursive function breaks when () included in call

From Dev

JavaScript - recursive call partial applied function

From Dev

How to Stop a Recursive Function in JavaScript?

From Dev

How to make a "call with reference" to recursive MatLab function

From Dev

How to properly call a recursive function inside a for loop?

From Dev

How do I return javascript data from a function using a recursive call

From Dev

Javascript recursive function - call to asynchronous function at the leave level

From Dev

Recursive Function Call Polymer

From Dev

JS Recursive function call

From Dev

Recursive Function Call Polymer

From Dev

How to execute a function on first call in a recursive template function?

From Dev

javascript recursive function: Uncaught RangeError: Maximum call stack size exceeded

From Dev

Recursive javascript function causing "Maximum call stack size exceeded"

From Dev

Recursive call in JavaScript not working?

From Dev

Understanding how a recursive function works in javascript

From Dev

Call recursive function in std::function

From Dev

How to call JavaScript function in VBA?

From Dev

How to call the jquery function in JavaScript?

From Dev

How to call nested function in javascript?

From Dev

How to call nested function in javascript?

From Dev

How to call JavaScript function in VBA?

From Dev

How to call a recursive linked list traversal function in C++

From Dev

How to fix an UnboundLocalError caused due to a recursive function call in Python?

From Dev

How to keep track of recursive call on my function collatz?

From Dev

How to call a function after finishing recursive asynchronous jobs in Python?

From Dev

How to fix an UnboundLocalError caused due to a recursive function call in Python?

Related Related

  1. 1

    Javascript: How to write and call a recursive function?

  2. 2

    From a cloud function: how to call JavaScript function that is recursive?

  3. 3

    From a cloud function: how to call JavaScript function that is recursive?

  4. 4

    JavaScript recursive function breaks when () included in call

  5. 5

    JavaScript - recursive call partial applied function

  6. 6

    How to Stop a Recursive Function in JavaScript?

  7. 7

    How to make a "call with reference" to recursive MatLab function

  8. 8

    How to properly call a recursive function inside a for loop?

  9. 9

    How do I return javascript data from a function using a recursive call

  10. 10

    Javascript recursive function - call to asynchronous function at the leave level

  11. 11

    Recursive Function Call Polymer

  12. 12

    JS Recursive function call

  13. 13

    Recursive Function Call Polymer

  14. 14

    How to execute a function on first call in a recursive template function?

  15. 15

    javascript recursive function: Uncaught RangeError: Maximum call stack size exceeded

  16. 16

    Recursive javascript function causing "Maximum call stack size exceeded"

  17. 17

    Recursive call in JavaScript not working?

  18. 18

    Understanding how a recursive function works in javascript

  19. 19

    Call recursive function in std::function

  20. 20

    How to call JavaScript function in VBA?

  21. 21

    How to call the jquery function in JavaScript?

  22. 22

    How to call nested function in javascript?

  23. 23

    How to call nested function in javascript?

  24. 24

    How to call JavaScript function in VBA?

  25. 25

    How to call a recursive linked list traversal function in C++

  26. 26

    How to fix an UnboundLocalError caused due to a recursive function call in Python?

  27. 27

    How to keep track of recursive call on my function collatz?

  28. 28

    How to call a function after finishing recursive asynchronous jobs in Python?

  29. 29

    How to fix an UnboundLocalError caused due to a recursive function call in Python?

HotTag

Archive