Angularjs将一个子对象添加到Json对象

时间:2017-05-26 13:48:30

标签: javascript angularjs json addchild

当我尝试将Children添加到Json对象时 我遇到了一个问题:

  

TypeError:ctrl.otsact.tests.push不是函数

脚本:

function addOTSACT() {
        var data = {
            'date': '',
            'subjects': angular.copy(ctrl.otsactSubjectsCopy)
        }
        $('.dateField').datepicker({
        changeMonth: true,
        changeYear: true
        });
        ctrl.otsact.tests.push({});
    }

Json对象:

{  
   "ACT":{  
      "name":"ACT",
      "tests":{  
         "73":{  
            "id":73,
            "official_test_id":1,
            "student_id":165888,
            "test_date":"2017-05-12",
            "score":"0.0",
            "created_at":"2017-05-23 13:50:40",
            "created_by_id":2766,
            "updated_at":"2017-05-23 13:50:40",
            "updated_by_id":2766,
            "subjects":[  
               {  
                  "id":1,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":1,
                  "score":1,
                  "student_score_id":73,
                  "name":"English",
                  "is_consider":1
               },
               {  
                  "id":2,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":2,
                  "score":1,
                  "student_score_id":73,
                  "name":"Math",
                  "is_consider":1
               },
               {  
                  "id":3,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":3,
                  "score":1,
                  "student_score_id":73,
                  "name":"Reading",
                  "is_consider":1
               },
               {  
                  "id":4,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":4,
                  "score":1,
                  "student_score_id":73,
                  "name":"Science",
                  "is_consider":1
               },
               {  
                  "id":5,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":5,
                  "score":1,
                  "student_score_id":73,
                  "name":"Writing",
                  "is_consider":0
               }
            ]
         },
         "74":{  
            "id":74,
            "official_test_id":1,
            "student_id":165888,
            "test_date":"2017-05-12",
            "score":"0.0",
            "created_at":"2017-05-23 13:50:40",
            "created_by_id":2766,
            "updated_at":"2017-05-23 13:50:40",
            "updated_by_id":2766,
            "subjects":[  
               {  
                  "id":1,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":1,
                  "score":2,
                  "student_score_id":74,
                  "name":"English",
                  "is_consider":1
               },
               {  
                  "id":2,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":2,
                  "score":2,
                  "student_score_id":74,
                  "name":"Math",
                  "is_consider":1
               },
               {  
                  "id":3,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":3,
                  "score":2,
                  "student_score_id":74,
                  "name":"Reading",
                  "is_consider":1
               },
               {  
                  "id":4,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":4,
                  "score":2,
                  "student_score_id":74,
                  "name":"Science",
                  "is_consider":1
               },
               {  
                  "id":5,
                  "official_test_id":1,
                  "student_id":165888,
                  "official_test_subject_id":5,
                  "score":2,
                  "student_score_id":74,
                  "name":"Writing",
                  "is_consider":0
               }
            ]
         }
      }
   }
}

我想添加75个孩子来测试孩子

2 个答案:

答案 0 :(得分:1)

您的tests媒体资源是...... object,而不是array。测试应该是tests: [],而不是tests: {}

答案 1 :(得分:1)

在您的JSON测试中,不是数组而是对象,并且您无法推送到对象。所以这一行是错误的:

ctrl.otsact.tests.push({});

您必须将其更改为:

ctrl.otsact.tests[MYNEWID] = MYNEWOBJECT;