【问题标题】:not able to iterate through array of arrays in javascript - something wrong with parent array无法遍历 javascript 中的数组数组 - 父数组有问题
【发布时间】:2022-01-11 19:11:12
【问题描述】:

我通过 api 调用创建了一个数组数组。父数组是 arrStories。当我在 console.log 中显示父数组时,它看起来像这样:

[]

当我单击[] 旁边的小箭头时,我得到了所有子数组。它们每个看起来都类似于:

0: Array(4)
0: "08 Apr 2019"
1: "Avoiding those surprise bills when you stumble out of your insurance network"
2: "https://url"
3: "https://url-of-image"
length: 4
[[Prototype]]: Array(0)

当我尝试获取父数​​组的长度时:

console.log(arrStories.length); //returns 0

发生了什么事??

这是我填充数组的方式:

$.getJSON('my-api', function(data) {
  getStories(data); //push stories into an array of arrays
});

orderStories(arrStories); //order by date descending

function getStories(data) {
  for (var i = 0; i < data.articles.length; i++) {
    var pubDate = data.articles[i].publishedDate;
    pubDate = pubDate.slice(5);
    pubDate = pubDate.substring(0, pubDate.length - 15);
    arrStory.push(pubDate, data.articles[i].title, data.articles[i].link, data.articles[i].images[0].url); //push values into array
    arrStories.push(arrStory); //push array into parent array
    arrStory = []; //clear original array
  }
}

function orderStories(arrStories) {
  arrStories.sort(function(a, b) {
    var dateA = new Date(a.date),
      dateB = new Date(b.date)
    return dateA - dateB;
  });
}

【问题讨论】:

  • 请将您的代码更改为带有示例对象的minimal reproducible example - 我让您成为了一个 sn-p。将$.getJSON('my-api', function(data) { getStories(data); //push stories into an array of arrays }); 改为示例
  • 异步请求 101 你点了一个比萨,然后在制作和交付之前尝试吃掉它

标签: javascript multidimensional-array


【解决方案1】:

我认为这是您的 $.getJSON 回调发生的问题您在下一行同步调用 orderStories -- 我认为您想将 orderStories 放在 $.getJSON 回调中

我想你在收到故事之前就已经订购了

另一种方法可能看起来像这样

async function getData () {
  return new Promise(resolve =>  { $.getJSON('my-api', resolve) };
}

getData().then(data => {
  const stories = getStories(data);
  const orderedStories = getOrderedStories(stories);
});

【讨论】:

    【解决方案2】:

    您没有带有 .date 的对象,您需要在 getJSON 函数中处理所有内容

    你可能想要这个

    function getStories(data) {
      return data.map(article => {
        var pubDate = data.articles[i].publishedDate;
        pubDate = pubDate.slice(5);
        pubDate = pubDate.substring(0, pubDate.length - 15);
        return {
          date: pubDate,
          title: article.title,
          link: article.link,
          imageUrl: article.images[0].url
        }
      })
    }
    
    
    function orderStories(arrStories) {
      arrStories.sort(function(a, b) {
        var dateA = new Date(a.date),
          dateB = new Date(b.date)
        return dateA - dateB;
      });
    }
    $.getJSON('my-api', function(data) {
      const arrStories = getStories(data)
      orderStories(arrStories)
      // here you can process arrStories
    });
    

    【讨论】:

    • 我收到消息“未捕获的类型错误:data.map 不是函数”。
    • @LauraNMS 我需要发布一个示例对象,以便我可以修复我的代码以处理您的数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2013-06-11
    • 2022-01-08
    • 2019-08-29
    • 1970-01-01
    • 2016-08-07
    相关资源
    最近更新 更多