我有这个JSON:
{
"id": 10,
"name": "Color test2",
"seed_id": 1,
"season_id": 2,
"description": null,
"work_schedule_type_id": 2,
"color": "#00A946",
"active": false,
"starred": true,
"phases": [{
"id": 2,
"name": "Phase2",
"work_schedule_type_id": 2,
"phase_color": "#343434",
"phase_order_of_operations": 1,
"description": "TEST PHASE TYPE2",
"isExpanded": false,
"$$hashKey": "object:1387"
}]
}
我需要分阶段为每个阶段添加一个属性tasks
。我怎么能这样做?
答案 0 :(得分:2)
span
答案 1 :(得分:1)
此方法添加了数组中的tasks
。
var obj = {
"id": 10,
"name": "Color test2",
"seed_id": 1,
"season_id": 2,
"description": null,
"work_schedule_type_id": 2,
"color": "#00A946",
"active": false,
"starred": true,
"phases": [{
"id": 2,
"name": "Phase2",
"work_schedule_type_id": 2,
"phase_color": "#343434",
"phase_order_of_operations": 1,
"description": "TEST PHASE TYPE2",
"isExpanded": false,
"$$hashKey": "object:1387"
}]
}
var tasks = ["Task1", "Task2"];
obj.phases.forEach(p => p.tasks = tasks);
console.log(obj.phases);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 2 :(得分:0)
假设您的JSON对象名为json,代码看起来像这样:
for (phase of json.phases) {
phase.task = "somevalue";
}