SukieC
SukieC

Reputation: 75

working with API json response - how do I extract nested array data and store it in a JavaScript array?

Below is a screenshot of the API response (from console). I need to extract the text of each "outcome" and store it in a JavaScript array.

enter image description here

I can iterate through the "Outcome" array, but how do I drill down further to get to the text (value) of each outcome? I feel like I'm missing something simple...

function processData(myObj) { //this is the callback function for the API ajax request
  var course_learning_outcomes_array = [];
  for (var k = 0; k < myObj.catalog.Course.Outcomes_Folder.Outcome.length; k++) {
    course_learning_outcomes_array.push(myObj.catalog.Course.Outcomes_Folder.Outcome[k]);
  }
  console.log(course_learning_outcomes_array);
}

Upvotes: 0

Views: 391

Answers (2)

Sebastian Richner
Sebastian Richner

Reputation: 760

You can access each element in the array and then simply push the outcome property, as follows:

function processData(myObj) { //this is the callback function for the API ajax request
  var course_learning_outcomes_array = [];
  for (var k = 0; k < myObj.catalog.Course.Outcomes_Folder.Outcome.length; k++) {
    course_learning_outcomes_array.push(myObj.catalog.Course.Outcomes_Folder.Outcome[k].outcome);
  }
  console.log(course_learning_outcomes_array);
}

Upvotes: 1

maio290
maio290

Reputation: 6742

Outcome[k].outcome

It's basically just a property of the object stored into the Outcome[].

Upvotes: 1

Related Questions