[英]How to call a recursive function in javascript
In my componentList
I am going to have multiple objects. 在我的
componentList
我将有多个对象。
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 $scope.componentList
格式如下
[
{
"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. 在上面的代码中,仅对父母进行了验证,因为forEach循环仅考虑
$scope.componentList
列表,而不考虑内部items[]
列表。 I want to call validateAdmincomp
function for each object. 我想为每个对象调用
validateAdmincomp
函数。 How can I call validateAdmincomp
function forEach object. 如何为每个对象调用
validateAdmincomp
函数。
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
});
}
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.
它将迭代每个键,如果键是一个对象而不是null,它将使用该对象调用该函数。
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); })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.