gpt4 book ai didi

javascript - Angularjs 将子对象添加到 Json 对象

转载 作者:行者123 更新时间:2023-12-01 03:36:51 25 4
gpt4 key购买 nike

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

TypeError: ctrl.otsact.tests.push is not a function

脚本:

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 个 child 来测试 child

最佳答案

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

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

您必须将其更改为:

ctrl.otsact.tests[MYNEWID] = MYNEWOBJECT;

关于javascript - Angularjs 将子对象添加到 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44203283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com