Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

250
Visualizações
JavaScript: How do I sort times in a nested array?

I'm working with a JSON file that contains the 2021 NFL schedule. I'm able to easily filter out Thursday, Sunday & Monday games. The filtering method returns my Sunday games in an array of objects.

Below is a look at how my Sunday array is set up. This code just shows the first two weeks for the Packers, but there are 18 weeks in the schedule, and I have the same info for each team.

So for the first week, for example, I'll have an array of 14 games. (The 14 teams are the visiting teams.) I need to loop through the games for the desired week and sort the array by the time of the games. A typical Sunday will have games at 12:00 PM, 3:25 PM, and 7:15 PM. There are other starting times throughout the season.

[
    {
      "name": "Green Bay Packers",
      "id": 1,
      "nickname": "Packers",
      "homefield": "Lambeau Field",
      "division": "NFC North",
      "schedule": [
        {
          "week": "1",
          "date": "Sept. 12",
          "weekday": "Sunday",
          "time": "3:25 PM",
          "network": "Fox",
          "opponent": "New Orleans Saints",
          "homeAway": "away",
          "venue": "Mercedes-Benz Superdome"
        },
        {
          "week": "2",
          "date": "Sept. 19",
          "weekday": "Monday",
          "time": "7:15 PM",
          "network": "ESPN",
          "opponent": "Detroit Lions",
          "homeAway": "home",
          "venue": "Lambeau Field"
        },
    etc.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Figured it out. "su" is my array of Sunday games, so I pass that and the week number into my function.

function sortSundayByTime(su, weekNo) {
  su.sort((a, b) => {
    if (a.schedule[weekNo].time < b.schedule[weekNo].time) {
      return -1;
    } else if (a.schedule[weekNo].time > b.schedule[weekNo].time) {
      return 1;
    }
  });
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda