Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
not able to iterate through array of arrays in javascript - something wrong with parent array

I created an array of arrays from an api call. The parent array is arrStories. When I display the parent array in console.log, it looks like this:

[]

When I click the little arrow next to [] I get all the child arrays. They each look similar to this:

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)

When I try to get the length of the parent array, though:

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

What has happened??

Here is how I filled my arrays:

$.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;
  });
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You do not have an object with .date and you need to process all inside the getJSON function

You likely want this

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
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

I think it's an issue of your $.getJSON callback happening after you're synchronously calling orderStories on the next line -- I think you want to put orderStories in the $.getJSON callback

I think you're ordering the stories before you've received them

Another approach could look something like this

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

getData().then(data => {
  const stories = getStories(data);
  const orderedStories = getOrderedStories(stories);
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda