【问题标题】:working with API json response - how do I extract nested array data and store it in a JavaScript array?使用 API json 响应 - 如何提取嵌套数组数据并将其存储在 JavaScript 数组中?
【发布时间】:2021-04-29 16:05:44
【问题描述】:

以下是 API 响应的屏幕截图(来自控制台)。我需要提取每个“结果”的文本并将其存储在 JavaScript 数组中。

我可以遍历“结果”数组,但如何进一步深入了解每个结果的文本(值)?我觉得我错过了一些简单的东西......

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

【问题讨论】:

    标签: arrays json api iteration response


    【解决方案1】:
    Outcome[k].outcome
    

    它基本上只是存储在 Outcome[] 中的对象的一个​​属性。

    【讨论】:

    • 是的,很简单!非常感谢@maio290。我会接受这个作为答案。
    【解决方案2】:

    您可以访问数组中的每个元素,然后简单地推送outcome 属性,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2019-12-22
      • 2016-06-07
      • 2019-09-18
      • 2017-02-21
      • 2019-09-16
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多