在我的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;
}
};
$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"
}
]
在上面的代码中,父类仅被验证为forEach循环仅考虑$scope.componentList
列表而不考虑内部items[]
列表。我想为每个对象调用validateAdmincomp
函数。
如何为每个对象调用validateAdmincomp
函数。
答案 0 :(得分:2)
使用以下代码。将迭代逻辑移动到方法(递归方法)
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
});
}
答案 1 :(得分:1)
我不确定你是否在寻找这个。它将迭代每个键,如果它是一个对象而不是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);
})