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

211
Visualizações
Console log shows 'undefined' for a specific value of an array

I am trying to store the values that correspond to Sundays and Saturdays for the current month in an array (arr) and display it in the console, the variable 'd' represents the number of days in the current month.

When I try to display a specific value like console.log(arr[1]) it shows "undefined" in the console.

let arr = [];
console.log(arr);
console.log(arr[0]);

const renderDay = () => {
  let td = [];
  for (let i = 1; i <= d; i++) {
    if (
      days[i].toLocaleDateString("fr-FR", options).slice(0, 3) === "sam" ||
      days[i].toLocaleDateString("fr-FR", options).slice(0, 3) === "dim"
    ) {
      td.push(
        <td className="test" key={i}>
          {days[i].toLocaleDateString("fr-FR", options).slice(0, 3)}
        </td>
      );
      arr.push(i);
    } else
      td.push(
        <td key={i}>
          {days[i].toLocaleDateString("fr-FR", options).slice(0, 3)}
        </td>
      );
  }
  return td;
};

I wanted to check if the array is empty, so I display the whole array with console.log(arr),the output looks fine, it shows an array with length: 8 arr = {0: 6 , 1: 7 , 2: 13 , 3: 14 , 4: 20 , 5: 21 , 6: 27 , 7: 28}, who represents the index of Saturdays and Sundays in the current month, but I can't access to every specific value by index.

How I can access to every specific value by its index (arr[0],arr[1],arr[2]....)?

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

0

I think you are calling console.log too early. console.log(arr) only works, because a reference to that array is logged, which is updated as soon as renderDay is run. Check MDN here: console.log documentation

Please be warned that if you log objects in the latest versions of Chrome and Firefox what you get logged on the console is a reference to the object, which is not necessarily the 'value' of the object at the moment in time you call console.log(), but it is the value of the object at the moment you open the console.

Try logging arr[0] after you called renderDay somewhere in your code.

You can also try to replace console.log(arr) with console.log(arr.toString()) to get the value arr has at the exact moment you log it - it should be empty when being called before renderDay is run.

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