0

I'm developing a ReactJS app and have data stored in an array. The array is non-empty, but for some reason it insists that arr[0] is undefined and arr.length is 0.

Edit: here's the result of the following four lines:

console.log(JSON.stringify(this.state.arr));
console.log(this.state.arr);
console.log(this.state.arr.length);
console.log(this.state.arr[0]);

enter image description here

Technicolor
  • 1,589
  • 2
  • 17
  • 31
  • Can you post what you have tried? – Naftali Mar 14 '19 at 20:40
  • A snippet would be helpful – Brian Le Mar 14 '19 at 20:42
  • 3
    The array is logged as a reference, so it will be updated in the console when it's updated in your code. You could try `console.log(JSON.stringify(arr))` to see what it looks like at the time of the logging. – Tholle Mar 14 '19 at 20:42
  • In terms of a solution? I'm honestly baffled. I've tried `console.log(arr.length)` and `console.log(arr[0])` right after this and it's still no good. I can update the question with a screenshot of the output. – Technicolor Mar 14 '19 at 20:43
  • You see the *actual output at that time* in the logged line, it is `[]` ... an empty array – Jonas Wilms Mar 14 '19 at 20:47
  • @Tholle yeah, looks like it is actually empty. I'm going to dig through my code and see what could be wrong but unfortunately there's too much code to post. – Technicolor Mar 14 '19 at 20:47
  • This happens when you try to access the array before it was populated, which means there is likely an async process and you try to access the array before that process finished. – Felix Kling Mar 14 '19 at 21:08

0 Answers0