0

I have this 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"
}]

}

I need add a property tasks for each phase in phases. How I can do this?

3

3 Answers 3

2
angular.forEach(obj.phases, function(item) { item.tasks = your_val } );
Sign up to request clarification or add additional context in comments.

Comments

1

This approach adds the tasks from an Array.

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; }

Comments

0

Assuming your JSON object is named json, the code would look something like this:

for (phase of json.phases) {
    phase.task = "somevalue";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.