0

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.

5
  • Is validateAdmincomp your code? If yes, then you can modify it to validate a property and call itself if it's an object. Commented Aug 17, 2017 at 5:54
  • Do you want to only use forEach()? Commented Aug 17, 2017 at 5:55
  • share you validateAdmincomp function code. Commented Aug 17, 2017 at 5:56
  • @Md.AtiqulIslam Not like that Commented Aug 17, 2017 at 5:56
  • @GauravSrivastava I have added the validateAdmincomp function Commented Aug 17, 2017 at 5:58

2 Answers 2

2

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
    });
}
Sign up to request clarification or add additional context in comments.

Comments

1

I am not sure you are looking for this or not. It will iterate each key and if its an object and not null it will call the function with the object.

var list = [
  {
    "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"
  }
]

function rec(obj){
 for (let key in obj){
  if(typeof obj[key] == "object" && obj[key]){
  console.log(key);
    rec(obj[key]);
  }
 }
}

list.forEach(function(o){
rec(o);
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.