Murali Murali - 2 months ago 14
JSON Question

How to call a recursive function in javascript

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.

Answer Source

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
    });
}