如何在angularjs控制器中映射json数组

时间:2016-10-16 07:59:49

标签: angularjs json

我在angularjs中映射json数组时遇到问题,有人可以看看我如何正确映射数组并迭代它们的值。在数组icdCode需要是动态字段(这是我需要帮助的另一部分)我怎样才能正确实现这一点。感谢

我在Controller中的映射

  $scope.preAuthorizationInfo.collections.preAuthClinicalDetailInfoFormVO.active = active;

Json数组

 "preAuthDiagnosisVOs": [
              {
                "preauthDiagnosisId": 165,
                "diagnosisVO": {
                  "diagnosisId": 171,
                  "diagnosisCode": "Provisional",
                  "icdCode": {
                    "icdCodeId": 1,
                    "description": "Other intestinal Escherichia coli infections",
                    "icdCode": "Other intestinal Escherichia coli infections",
                    "icdCodeChapter": "Certain infectious and parasitic diseases",
                    "icdCodeCode": "A04.4"
                  },
                  "active": false
                },
                "active": true
              },
              {
                "preauthDiagnosisId": 166,
                "diagnosisVO": {
                  "diagnosisId": 172,
                  "diagnosisCode": "differential",
                  "icdCode": {
                    "icdCodeId": 2,
                    "description": "Other viral enteritis",
                    "icdCode": "Other viral enteritis",
                    "icdCodeChapter": "Certain infectious and parasitic diseases",
                    "icdCodeCode": "A08.3"
                  },
                  "active": false
                },
                "active": true
              }
            ]
          },

2 个答案:

答案 0 :(得分:0)

如果要迭代此对象,可以使用嵌套循环

angular.forEach(your_object.preAuthDiagnosisVOs, function(value, key) {
    if (angular.isObject(value)) {
        angular.forEach(value, function(value1, key) {
            if (angular.isObject(value1)) {
                angular.forEach(value1, function(value2, key) {
                    if (angular.isObject(value2)) {
                        angular.forEach(value2, function(value3, key) {
                            console.log(key + " : " + value3);
                        });
                    } else {
                        console.log(key + " : " + value2);
                    }
                });
            } else {
                console.log(key + " : " + value1);
            }
        });
    } else {
        console.log(key + " : " + value);
    }
});

如果您只想使用该值,请使用

yourObject.preAuthDiagnosisVOs[0].activeyourObject.preAuthDiagnosisVOs[1].active

希望这会对你有所帮助。感谢

答案 1 :(得分:0)

我只是验证你的json格式它有一些语法问题。我纠正了这个。

{
    "preAuthDiagnosisVOs": [{
        "preauthDiagnosisId": 165,
        "diagnosisVO": {
            "diagnosisId": 171,
            "diagnosisCode": "Provisional",
            "icdCode": {
                "icdCodeId": 1,
                "description": "OtherintestinalEscherichiacoliinfections",
                "icdCode": "OtherintestinalEscherichiacoliinfections",
                "icdCodeChapter": "Certaininfectiousandparasiticdiseases",
                "icdCodeCode": "A04.4"
            },
            "active": false
        },
        "active": true
    }, {
        "preauthDiagnosisId": 166,
        "diagnosisVO": {
            "diagnosisId": 172,
            "diagnosisCode": "Provisional",
            "icdCode": {
                "icdCodeId": 2,
                "description": "Otherviralenteritis",
                "icdCode": "Otherviralenteritis",
                "icdCodeChapter": "Certaininfectiousandparasiticdiseases",
                "icdCodeCode": "A08.3"
            },
            "active": false
        },
        "active": true
    }]
}

现在访问有效键。 preAuthDiagnosisVOs 是要访问的数组,您必须使用角度 forEach 循环进行迭代。对于每个索引的访问活动键,只需迭代并获取它或直接使用object.preAuthDiagnosisVOs[0].active and object.preAuthDiagnosisVOs[1].active

我认为它可能对你有所帮助。谢谢