0

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]....)?

Danry
  • 147
  • 15
user6354
  • 63
  • 6
  • First of all, it's undefined because you just defined it. `let arr = []; console.log(arr);` will always log `[]` without a fair amount of work and luck. Secondly, if `days` contains `Date` objects, it would be far easier to do `if ([0,6].includes(days[i].getDay()))` to find out if it's a weekend day that formatting the date twice. Finally, what's `arr2`? It's not shown in your code, so I'm not sure where it's coming from. – Heretic Monkey Nov 04 '21 at 19:26
  • There many undefined variable on your code, Fix them up. – Fritzdultimate Nov 04 '21 at 19:28
  • @HereticMonkey Yes `days` contains `Date` objects, i tried something like this `if(days[i].getDay()%6==0)` and it works fine ,what I can't understand is why when I type `console.log(arr)` it shows in the console all the weekend day in the month but when I type `console.log(arr[1])` it shows in the console undefined. I made a mistake when i posted `arr2` means `arr`,I just edited that in the post – user6354 Nov 04 '21 at 19:53
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 05 '21 at 09:41
  • You can't access any variable's contents before it is populated. Simple as that. It is irrelevant whether it's by index or otherwise. See the answers to [weird array behaviour in javascript](https://stackoverflow.com/q/49838597/215552) as to why you see the data after the fact. – Heretic Monkey Nov 05 '21 at 10:16
  • @HereticMonkey This answer a part of my question,I understand now why `console.log(arr)` works and `console.log(arr[index])` can't work,but I still not get how I can access to every element in the array before I call the function `rendeDay` – user6354 Nov 05 '21 at 19:03
  • How would eat a slice of a pizza before it's delivered to you? – Heretic Monkey Nov 05 '21 at 19:38

1 Answers1

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.

Thomas Altmann
  • 1,744
  • 2
  • 11
  • 16
  • that's really works,when I try logging `arr[0]` after i called `rendeDay`,is there a way to make the values in `arr` accessible by their index,I mean `arr[0] arr[1]` .... etc before I called renderDay,because I need to use them in other functions – user6354 Nov 04 '21 at 21:32
  • The array will be empty because `renderDay` is responsible for populating the array with values. – Thomas Altmann Nov 05 '21 at 08:02